<?php

/**
 * @Author: liu
 * @Date:   2018-01-31 09:49:35
 * @Last Modified by:   liu
 * @Last Modified time: 2018-10-16 15:52:28
 * 用于采购相关的 控制器
 */
class ScsPurchaseController extends BaseController {
    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-31T15:14:53+0800
     * @return   [type]                   [description]
     * 通过预警创建采购单
     */
    public function warn_create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'type' => 'required|in:1,2',//1资材 2集采
                'args' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $type = Input::get('type');
            $args = Input::get('args'); 
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $response = DB::transaction(function() use ($sc_user,$type,$args){
                $pms_code =Common::createOrderId(); //'采购单号',
                $pms_order = new PMSOrder();
                $pms_order ->pms_code = $pms_code;
                $pms_order ->source = 1;
                $pms_order ->created_by = $sc_user->id;
                $pms_order ->created_type = 1;
                $pms_order ->shop_id = $sc_user->shop_id;
                $pms_order ->status = 1;
                $pms_order ->type =$type;
                $pms_order->save();


                $pms_order_flow = new PMSOrderFlow();
                $pms_order_flow->pms_code = $pms_code;
                $pms_order_flow->status =1;
                $pms_order_flow->operation =1;
                $pms_order_flow->created_by =$sc_user->id;
                $pms_order_flow->created_type = 1;
                $pms_order_flow->save();
                $ids=$args? json_decode($args,1):array();

                $total_amount=0;
                foreach ($ids as $_id) {
                    $dsi=SupplyItem::find($_id["id"]);
                    if(is_null($dsi)||$dsi->status!=1){
                        DB::rollback();
                        $response= Lang::get("scs_error.purchase_supply_tiem_error_65012");
                        $response["msg"]="id为".$_id["id"].",".$response["msg"];
                        return $response;
                    }
                    $pmsop=new PMSOrderProducts();
                    $pmsop->pms_code=$pms_code;
                    $pmsop->product_id=$_id["id"];
                    $pmsop->all_num=$_id["number"];
                    $pmsop->error_num=0;
                    $pmsop->status=1;
                    $pmsop->created_by = $sc_user->id;
                    $pmsop->created_type = 1;                
                    $pmsop->save();
                    $total_amount+=$_id["number"];
                }        
                PMSOrder::where("pms_code",$pms_code)->update(array("all_num"=>$total_amount));
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$pms_code));
            });           
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "warn_create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-01T14:02:15+0800
     * @return   [type]                   [description]
     * 门店的采购单列表
     */
    public function lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            // $pmso=PMSOrder::where("pms_order.shop_id",$sc_user->id);
            // $ids=DB::table("pms_order")->where
            $pmso=DB::table("pms_order")->where("pms_order.shop_id",$sc_user->shop_id);
            $pmso->leftJoin("sc_user","pms_order.created_by","=","sc_user.id")
                ->leftJoin("pms_user","pms_order.created_by","=","pms_user.id")
                ->leftJoin("pms_user as pm_au","pms_order.approve_by","=","pm_au.id")
                ->leftJoin("sc_user as se_au","pms_order.approve_by","=","se_au.id");

            if(Input::get("status") != "") {
                $pmso->where("pms_order.status",Input::get("status")); //期望日期开始
            }            
            if(Input::get("type") != "") {
                $pmso->where("pms_order.type",Input::get("type")); //期望日期开始
            }
            if(Input::get("source") != "") {
                $pmso->where("pms_order.source",Input::get("source")); //期望日期开始
            }
            if(Input::get("pms_code") != "") {
                $pmso->where("pms_order.pms_code","like",'%'.Input::get("pms_code").'%'); //单号
            }
            if(Input::get("hope_at_start") != "") {
                $pmso->where("pms_order.hope_at",">",Input::get("hope_at_start")." 00:00:00"); //期望日期开始
            }
            if(Input::get("hope_at_end") != "") {
                $pmso->where("pms_order.hope_at","<",Input::get("hope_at_end")." 23:59:59"); //期望日期结束
            }
            if(Input::get("year") != "") {
                $pmso->where("pms_order.created_at",">",Input::get("year")."-01-01 00:00:00"); //期望日期开始
                $pmso->where("pms_order.created_at","<",Input::get("year")."-12-31 23:59:59"); //期望日期结束
            }           

            $count = $pmso->count();
            $pmso = $pmso->select("pms_order.*","sc_user.real_name as ce_sce_name","pms_user.real_name as ce_pms_name","se_au.real_name as au_sce_name","pm_au.real_name as au_pms_name")->orderBy("pms_order.created_at","desc")->skip($page_size*($page-1))->take($page_size)->get();           

            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"purchase_order_lists"=>$pmso,'source'=>PMSOrder::$source,'type'=>PMSOrder::$type,'status'=>PMSOrder::$status));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchase_order_lists", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T14:50:18+0800
     * @return   [type]                   [description]
     * 采购单详情
     */
    public function purchaseorder_detail(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');

            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmo=PMSOrder::find($id);
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id) {
                $response = Lang::get("scs_error.purchase_order_notexit_65000");
                break;
            }          
            if($pmo->created_type==1){
                $sc_user=SCUser::find($pmo->created_by);
                $pmo->created_by_name=isset($sc_user->real_name)?$sc_user->real_name:'';
            }else{
                $pms_user=PMSUser::find($pmo->created_by);
                $pmo->created_by_name=isset($pms_user->real_name)?$pms_user->real_name:'';
            }
            if($pmo->approve_type==1){
                $sc_user=SCUser::find($pmo->approve_by);
                $pmo->approve_by_name=isset($sc_user->real_name)?$sc_user->real_name:'';
            }else{
                $pms_user=PMSUser::find($pmo->approve_by);
                $pmo->approve_by_name=isset($pms_user->real_name)?$pms_user->real_name:'';
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("purchase_order_info"=>$pmo,'source'=>PMSOrder::$source,'type'=>PMSOrder::$type,'status'=>PMSOrder::$status));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "detail", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-01T16:16:42+0800
     * @return   [type]                   [description]
     * 正常的创建采购单
     */
    public function purchaseorder_create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'type' => 'required|in:1,2',//1资材 2集采
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $type = Input::get('type');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $response = DB::transaction(function() use ($sc_user,$type){
                $pms_code =Common::createOrderId(); //'采购单号',
                $pms_order = new PMSOrder();
                $pms_order ->pms_code = $pms_code;
                $pms_order ->source = 1;
                $pms_order ->created_by = $sc_user->id;
                $pms_order ->created_type = 1;
                $pms_order ->shop_id = $sc_user->shop_id;
                $pms_order ->status = 1;
                $pms_order ->type =$type;
                $pms_order->save();


                $pms_order_flow = new PMSOrderFlow();
                $pms_order_flow->pms_code = $pms_code;
                $pms_order_flow->status =1;
                $pms_order_flow->operation =1;
                $pms_order_flow->created_by =$sc_user->id;
                $pms_order_flow->created_type = 1;
                $pms_order_flow->save();
                
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$pms_code));
            });           
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T09:43:30+0800
     * @return   [type]                   [description]
     * 采购单的取消
     */
    public function purchaseorder_cancel(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }

            $pmo=PMSOrder::find($id);
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                $response = Lang::get("scs_error.purchase_order_notexit_65000");
                break;
            }
            if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
                $response = Lang::get("scs_error.purchase_order_status_65001");
                break;
            }

            $pmo ->status = 0;
            $pmo->save();
            $pms_order_flow = new PMSOrderFlow();
            $pms_order_flow->pms_code = $pmo->pms_code;
            $pms_order_flow->status =0;
            $pms_order_flow->operation =0;
            $pms_order_flow->created_by =$sc_user->id;
            $pms_order_flow->created_type = 1;
            $pms_order_flow->save();

                
            $response= array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$pmo->pms_code));         
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_cancel", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }




    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-02T10:22:39+0800
     * @return   [type]                   [description]
     * 需求列表
     */
    public function requirements_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmso=PMSOrderProducts::where("pms_code",$id)->where("status",1)->select("id","product_id","all_num","last_num","ready_num","error_num","right_num","quotations_number")->get();
            foreach ($pmso as $_pmss) {
                $si_info=SupplyItem::getCacheItemInfo($_pmss->product_id);
                $_pmss->item_id=$si_info->id;
                $_pmss->item_type=$si_info->type;
                $_pmss->code=$si_info->code;
                $_pmss->old_code=$si_info->old_code;
                $_pmss->brand_name=$si_info->brand_name;
                $_pmss->series_name=$si_info->series_name;
                $_pmss->model=$si_info->model;
                $_pmss->specification=$si_info->specification;
                $_pmss->scf_id=$si_info->st_classifi_id;
                $_pmss->scf_name=$si_info->scf_name;
                $_pmss->unit=$si_info->unit;
                $_pmss->bz_unit=$si_info->bz_unit;
                $_pmss->volume_size=$si_info->pack;
                $_pmss->car_model='';
                $_pmss->car_model_comments=$si_info->car_model_comments;
                $_pmss->item_name=$si_info->name;
            }                   
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($pmso),"item_info"=>$pmso));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "requirements_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-02T11:08:36+0800
     * @return   [type]                   [description]
     * 用于更改采购单的预期时间
     */
    public function change_hope_at(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
                'data' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            $data = Input::get('data');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmo=PMSOrder::find($id);
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                $response = Lang::get("scs_error.purchase_order_notexit_65000");
                break;
            }
            if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
                $response = Lang::get("scs_error.purchase_order_status_65001");
                break;
            }
            $pmo->hope_at=$data;
            $pmo->save();                            
            $response= array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$id));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "change_hope_at", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-02T13:47:15+0800
     * @return   [type]                   [description]
     * 采购项目的报价详情
     */
    public function product_offer(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmop=PMSOrderProducts::find($id);
            if(is_null($pmop)) {
                $response = Lang::get("scs_error.purchase_order_product_notexit_65002");
                break;
            }
            $pmo=PMSOrder::find($pmop->pms_code);
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id) {
                $response = Lang::get("scs_error.purchase_order_notexit_65000");
                break;
            }
            // if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
            //     $response = Lang::get("scs_error.purchase_order_status_65001");
            //     break;
            // }
            $pmopo=PMSOrderProductOffer::where("pms_order_product_id",$id)->select("type","amount","name")->get();
            $temp_array=array();
            foreach ($pmopo as $_pmopo) {
                $temp_array[$_pmopo->type]=$_pmopo;
            }
            $finally=array();
            foreach (PMSOrderProductOffer::$type as $key=>$value) {
                $finally[]=array(
                    "type"=>$key,
                    "name"=>$value,
                    "amount"=>isset($temp_array[$key])?$temp_array[$key]->amount:'',
                    "supply_name"=>isset($temp_array[$key])?$temp_array[$key]->name:'',
                );
            }                 
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("item_info"=>$finally));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "product_offer", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-02T14:17:57+0800
     * @return   [type]                   [description]
     * 报价的保存
     */
    public function offer_save(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            $args = Input::get('args'); //格式如下 [{"type":1,"name":"lyt","amount":11}]
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $response = DB::transaction(function() use ($sc_user,$id,$args){
                $pmop=PMSOrderProducts::find($id);
                if(is_null($pmop)) {
                    return Lang::get("scs_error.purchase_order_product_notexit_65002");
                }
                $pmo=PMSOrder::find($pmop->pms_code);
                if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->type==1) {
                    return Lang::get("scs_error.purchase_order_notexit_65000");
                }
                if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
                    return Lang::get("scs_error.purchase_order_status_65001");
                }

                $args_array=$args? json_decode($args,1):array();
                PMSOrderProductOffer::where("pms_order_product_id",$id)->delete();
                foreach ($args_array as $_args) {
                    // $popf=PMSOrderProductOffer::where("pms_order_product_id",$id)->where("type",$_args["type"])->first();
                    // if($popf){
                    //     $popf->amount=$_args["amount"]*100;
                    //     $popf->name=$_args["name"];
                    //     $popf->updated_type=1;
                    //     $popf->updated_by=$sc_user->id;
                    //     $popf->save();
                    // }else{
                        $popf=new PMSOrderProductOffer();
                        $popf->pms_order_product_id=$id;
                        $popf->type=$_args["type"];
                        $popf->amount=$_args["amount"]*100;
                        $popf->name=$_args["name"];
                        $popf->created_type=1;
                        $popf->created_by=$sc_user->id;
                        $popf->updated_type=1;
                        $popf->updated_by=$sc_user->id;
                        $popf->save();
                    // }
                }        
                $number=PMSOrderProductOffer::where("pms_order_product_id",$id)->count();
                $pmop->quotations_number=$number;
                $pmop->save();
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_product_id"=>$id));
            });    
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "offer_save", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T10:20:09+0800
     * @return   [type]                   [description]
     * 采购单搜索耗材
     */
    public function supply_search(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            $retrieval_code=Input::get('retrieval_code');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $pmso=PMSOrderProducts::where("pms_code",$id)->where("status",1)->select("product_id")->get();//采购单已经存在的需求列表
            $ids=array();
            foreach ($pmso as $_pmso) {
                $ids[]=$_pmso->product_id;
            }
          
            $si=SupplyItem::where("status",1);
            if($retrieval_code){
                $si=$si->where("supply_item.retrieval_code","like","%".$retrieval_code."%");                
            }
            $si=$si->take(100+sizeof($ids))->select("id")->get();//获取 100条加上 已经存在的条数 
            
            $finally_ids=array();
            foreach ($si as $_si) {
                if(!in_array($_si->id,$ids)){
                    $finally_ids[]=$_si->id;
                }
            }           

            $finally_array=array();
            foreach ($finally_ids as $_fi) {
                $si_info=SupplyItem::getCacheItemInfo($_fi);
                $temp_array=array(
                    "item_id"=>$si_info->id,
                    "item_type"=>$si_info->type,
                    "code"=>$si_info->code,
                    "item_name"=>$si_info->name,
                    "unit"=>$si_info->unit,
                    "bz_unit"=>$si_info->bz_unit,
                    "volume_size"=>$si_info->pack,
                );
                // $_pmss->item_id=$si_info->id;
                // $_pmss->item_type=$si_info->type;
                // $_pmss->code=$si_info->code;
                // $_pmss->old_code=$si_info->old_code;
                // $_pmss->brand_name=$si_info->brand_name;
                // $_pmss->series_name=$si_info->series_name;
                // $_pmss->model=$si_info->model;
                // $_pmss->specification=$si_info->specification;
                // $_pmss->scf_id=$si_info->st_classifi_id;
                // $_pmss->scf_name=$si_info->scf_name;
                // $_pmss->unit=$si_info->unit;
                // $_pmss->bz_unit=$si_info->bz_unit;
                // $_pmss->volume_size=$si_info->pack;
                // $_pmss->car_model='';
                // $_pmss->car_model_comments=$si_info->car_model_comments;
                // $_pmss->item_name=$si_info->name;
                $finally_array[]=$temp_array;
                if(sizeof($finally_array)>=100){
                    break;
                }
            }                
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("item_info"=>$finally_array));
            
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "supply_search", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T13:52:11+0800
     * @return   [type]                   [description]
     * 用于采购单添加采购项
     */
    public function purchaseorder_addsupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
                "item_id"=>"required",
                "number"=>"required",
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            $item_id=Input::get('item_id');
            $number=Input::get('number');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $response = DB::transaction(function() use ($sc_user,$id,$item_id,$number){                
                $pmo=PMSOrder::where("pms_code",$id)->lockForUpdate()->first();;
                if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                    return Lang::get("scs_error.purchase_order_notexit_65000");
                }
                if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
                    return Lang::get("scs_error.purchase_order_status_65001");
                }
                $pmsp=PMSOrderProducts::where("pms_code",$id)->where("status",1)->where("product_id",$item_id)->first();
                if ($pmsp) {//如果该订单存在改耗材的 
                    return Lang::get("scs_error.purchase_order_product_exit_65003");
                }

                $pmsop=new PMSOrderProducts();
                $pmsop->pms_code=$pmo->pms_code;
                $pmsop->product_id=$item_id;
                $pmsop->all_num=$number;
                $pmsop->error_num=0;
                $pmsop->status=1;
                $pmsop->created_by = $sc_user->id;
                $pmsop->created_type = 1;                
                $pmsop->save();
                // $total_amount=PMSOrder::where("pms_code",$pmo->pms_code)->where("status",1)->sum("all_num");       
                // PMSOrder::where("pms_code",$pmo->pms_code)->update(array("all_num"=>$total_amount,"error_num"=>$total_amount));
                $pmo->all_num=$pmo->all_num+$number;
                $pmo->error_num=0;
                $pmo->save();


                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$id));
            });            
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_addsupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T17:02:55+0800
     * @return   [type]                   [description]
     * 删除采购项的接口
     */
    public function purchaseorder_delsupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购项id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $response = DB::transaction(function() use ($sc_user,$id){                
                $pmop=PMSOrderProducts::find($id);
                if(is_null($pmop)) {
                    return Lang::get("scs_error.purchase_order_product_notexit_65002");
                }
                if($pmop->status!=1) {
                    return Lang::get("scs_error.purchase_order_product_notexit_65002");
                }
                $pmo=PMSOrder::find($pmop->pms_code);
                if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                    return Lang::get("scs_error.purchase_order_notexit_65000");
                }
                if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能修改采购时间
                    return Lang::get("scs_error.purchase_order_status_65001");
                }
                $pmop->status=0;     
                $pmop->save();
                // $total_amount=PMSOrder::where("pms_code",$pmo->pms_code)->where("status",1)->sum("all_num");       
                // PMSOrder::where("pms_code",$pmo->pms_code)->update(array("all_num"=>$total_amount,"error_num"=>$total_amount));
                $pmo->all_num=$pmo->all_num-$pmop->all_num;
                $pmo->save();
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$pmop->pms_code));
            });            
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_delsupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T15:34:30+0800
     * @return   [type]                   [description]
     * 提价采购单  
     * 该接口只能 提供门店的 集采提交确认的状态用
     */
    public function purchaseorder_submit(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $response = DB::transaction(function() use ($sc_user,$id){                
                $pmo=PMSOrder::where("pms_code",$id)->lockForUpdate()->first();
                if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                    return Lang::get("scs_error.purchase_order_notexit_65000");
                }
                if ($pmo->status!=1&&$pmo->status!=3) {//不是新建和驳回不能提交审核
                    return Lang::get("scs_error.purchase_order_status_65001");
                }
                if ($pmo->type!=1) {//只有集采才需要需要提交审核
                    return Lang::get("scs_error.purchase_order_type_error_65004");
                }
                $pmo->status =2;
                $pmo->save();
                $pms_order_flow=new PMSOrderFlow();
                $pms_order_flow->pms_code=$pmo->pms_code;
                $pms_order_flow->status=2;
                $pms_order_flow->operation=2;
                $pms_order_flow->created_by=$sc_user->id;
                $pms_order_flow->created_type = 1;
                $pms_order_flow->save();
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$id));
            });            
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_submit", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-04T16:15:13+0800
     * @return   [type]                   [description]
     * 该接口只能 提供门店 集采审核中的撤消
     */
    public function purchaseorder_resubmit(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }      
            $response = DB::transaction(function() use ($sc_user,$id){                
                $pmo=PMSOrder::where("pms_code",$id)->lockForUpdate()->first();
                if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id||$pmo->source==2) {
                    return Lang::get("scs_error.purchase_order_notexit_65000");
                }
                if ($pmo->status!=2) {//只能是审核中的 状态才能 取消审核
                    return Lang::get("scs_error.purchase_order_status_65001");
                }
                if ($pmo->type!=1) {//只有集采才需要需要提交审核
                    return Lang::get("scs_error.purchase_order_type_error_65004");
                }
                $pmspf=PMSOrderFlow::where("pms_code",$id)->whereIn("status",array(1,3))->orderBy("created_at","desc")->first();
                $pmo->status =$pmspf->status;
                $pmo->save();
                $pms_order_flow=new PMSOrderFlow();
                $pms_order_flow->pms_code=$pmo->pms_code;
                $pms_order_flow->status=$pmspf->status;
                $pms_order_flow->operation=3;
                $pms_order_flow->created_by=$sc_user->id;
                $pms_order_flow->created_type = 1;
                $pms_order_flow->save();
                return array('code' => 0, 'msg' => 'ok', 'result' => array("pms_order_id"=>$id));
            });            
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "purchaseorder_resubmit", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-06T09:48:33+0800
     * @return   [type]                   [description]
     */
    public function plan_product_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmo=PMSOrder::where("pms_code",$id)->first();
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id) {
                return Lang::get("scs_error.purchase_order_notexit_65000");
            }
            $pmspp=PMSPlanProducts::leftJoin("pms_plan_order","pms_plan_order.plan_code","=","pms_plan_products.plan_code")
                ->leftJoin("maintenance_supplier","maintenance_supplier.id","=","pms_plan_order.supplier_id")->where("pms_plan_products.pms_code",$id)->select("pms_plan_products.product_id","pms_plan_products.all_num","pms_plan_products.last_num","pms_plan_products.ready_num","pms_plan_products.tax_purchase_price","pms_plan_products.no_tax_purchase_price","pms_plan_order.plan_code","pms_plan_order.other_order_code","pms_plan_order.supplier_id","pms_plan_order.pay_type","maintenance_supplier.name as supplier_name","pms_plan_products.order_products_id")->get();
            $all_ding_id=array();
            $finally_array=array();
            foreach ($pmspp as $_pmspp) {
                $si_info=SupplyItem::getCacheItemInfo($_pmspp->product_id);
                $_pmspp->tax_purchase_price=round($_pmspp->tax_purchase_price/100,2);
                $_pmspp->no_tax_purchase_price=round($_pmspp->no_tax_purchase_price/100,2);
                $_pmspp->item_id=$si_info->id;
                $_pmspp->item_type=$si_info->type;
                $_pmspp->code=$si_info->code;
                $_pmspp->old_code=$si_info->old_code;
                $_pmspp->unit=$si_info->unit;
                $_pmspp->bz_unit=$si_info->bz_unit;
                $_pmspp->volume_size=$si_info->pack;
                $_pmspp->item_name=$si_info->name;
                $all_ding_id[]=$_pmspp->order_products_id;
                $finally_array[]=$_pmspp;
            }                   

            $pmso=PMSOrderProducts::where("pms_code",$id)->where("status",1)->select("id","product_id")->get();
            foreach ($pmso as $_pmso) {
                if(in_array($_pmso->id,$all_ding_id)){//如果已经订货 跳出
                    continue;
                }
                $si_info=SupplyItem::getCacheItemInfo($_pmso->product_id);
                $temp_boj=new \stdClass;
                $temp_boj->product_id=$_pmso->product_id;
                $temp_boj->all_num="";
                $temp_boj->last_num="";
                $temp_boj->ready_num="";
                $temp_boj->tax_purchase_price="";
                $temp_boj->no_tax_purchase_price="";
                $temp_boj->plan_code="";
                $temp_boj->other_order_code="";
                $temp_boj->supplier_id="";
                $temp_boj->pay_type=null;
                $temp_boj->supplier_name="";
                $temp_boj->order_products_id="";
                $temp_boj->item_id=$si_info->id;
                $temp_boj->item_type=$si_info->type;
                $temp_boj->code=$si_info->code;
                $temp_boj->old_code=$si_info->old_code;
                $temp_boj->unit=$si_info->unit;
                $temp_boj->bz_unit=$si_info->bz_unit;
                $temp_boj->volume_size=$si_info->pack;
                $temp_boj->item_name=$si_info->name;
                $finally_array[]=$temp_boj;
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($finally_array),"item_info"=>$finally_array));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "plan_product_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-06T13:07:52+0800
     * @return   [type]                   [description]
     * 收货明细
     */
    public function receive_product_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmo=PMSOrder::where("pms_code",$id)->first();
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id) {
                return Lang::get("scs_error.purchase_order_notexit_65000");
            }
            $pmspp=DB::table("pms_receive_products_result")->where("pms_receive_products_result.pms_code",$id);
            $pmspp=$pmspp->leftJoin("pms_receive_products","pms_receive_products.id","=","pms_receive_products_result.pms_receive_products_id")
                ->leftJoin("sc_user","pms_receive_products_result.created_by","=","sc_user.id")
                ->leftJoin("pms_user","pms_receive_products_result.created_by","=","pms_user.id")
                ->leftJoin("maintenance_storage_item","pms_receive_products_result.storage_id","=","maintenance_storage_item.id")
                ->select("pms_receive_products_result.num","pms_receive_products_result.storage_num","pms_receive_products_result.data_product_id","pms_receive_products_result.storage_list_id","pms_receive_products_result.product_id","pms_receive_products_result.comment","pms_receive_products_result.type","pms_receive_products_result.created_type","sc_user.real_name as ce_sce_name","pms_user.real_name as ce_pms_name","maintenance_storage_item.tax_purchase_price")->get();
            // Log::info();
            foreach ($pmspp as $_pmspp) {
                $si_info=SupplyItem::getCacheItemInfo($_pmspp->product_id);
                $_pmspp->tax_purchase_price=round($_pmspp->tax_purchase_price/100,2);
                $_pmspp->item_id=$si_info->id;
                $_pmspp->item_type=$si_info->type;
                $_pmspp->code=$si_info->code;
                $_pmspp->old_code=$si_info->old_code;
                $_pmspp->unit=$si_info->unit;
                $_pmspp->bz_unit=$si_info->bz_unit;
                $_pmspp->volume_size=$si_info->pack;
                $_pmspp->item_name=$si_info->name;
                $storage_si_info=SupplyItem::getCacheItemInfo($_pmspp->data_product_id);
                $_pmspp->storage_item_id=isset($storage_si_info)?$storage_si_info->id:"";
                $_pmspp->storage_item_type=isset($storage_si_info)?$storage_si_info->type:"";
                $_pmspp->storage_code=isset($storage_si_info)?$storage_si_info->code:"";
                $_pmspp->storage_old_code=isset($storage_si_info)?$storage_si_info->old_code:"";
                $_pmspp->storage_unit=isset($storage_si_info)?$storage_si_info->unit:"";
                $_pmspp->storage_bz_unit=isset($storage_si_info)?$storage_si_info->bz_unit:"";
                $_pmspp->storage_volume_size=isset($storage_si_info)?$storage_si_info->pack:"";
                $_pmspp->storage_item_name=isset($storage_si_info)?$storage_si_info->name:"";
            }                   
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($pmspp),"item_info"=>$pmspp,"type"=>PMSReceiveProductsResult::$type));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "receive_product_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-02-06T13:39:21+0800
     * @return   [type]                   [description]
     * 采购单的流转
     */
    public function flows_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',//采购单id
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }
            $pmo=PMSOrder::where("pms_code",$id)->first();
            if(is_null($pmo)||$pmo->shop_id!=$sc_user->shop_id) {
                return Lang::get("scs_error.purchase_order_notexit_65000");
            }
            $pmspp=DB::table("pms_order_flow")->where("pms_order_flow.pms_code",$id);
            $pmspp=$pmspp->leftJoin("sc_user","pms_order_flow.created_by","=","sc_user.id")
                ->leftJoin("pms_user","pms_order_flow.created_by","=","pms_user.id")->select("pms_order_flow.created_type","sc_user.real_name as ce_sce_name","pms_user.real_name as ce_pms_name","pms_order_flow.created_at","pms_order_flow.status","pms_order_flow.operation","pms_order_flow.comment")->orderBy("pms_order_flow.created_at","desc")->get();                            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($pmspp),"item_info"=>$pmspp,"status"=>PMSOrder::$status,"operation"=>PMSOrderFlow::$operation));
        } while (false);
        Log::info("info", array("system" => "scs" ,"model" => "purchase_order", "event" => "flows_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-4-10 下午2:50
     * @Description:     获取三级分类 [同时支持模糊查询]  三级分类独立分开
     * @param string $token             用户token
     * @param int $level                分类的等级 [1:一级分类;2:二级分类;3:三级分类;]
     * @param int $parent_id            父级分类id
     * @param string $key               分类的名称
     * @param int $num                  一次展示的数量 [ 默认为全部 ]
     * @link http://api.anhui.com/v1/scs/purchase/query_level?token=08b3fd6b460371cf81ef5760267996ee&level=1
     */
    public function query_level(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'level' => 'integer',
                'num' => 'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $level = Input::get('level', '');           //分类的等级 [1:一级分类;2:二级分类;3:三级分类;]
            $parent_id = Input::get('parent_id', '');   //父级id
            $key = Input::get('key', '');               //分类的名称
            $num = Input::get('num', '');               //一次展示的数量 [ 默认为全部 ]
            
            $list = SupplyClassification::select('id', 'name', 'parent_id', 'level', 'status', 'is_sale', 'retrieval_code')
                    ->where('status', 1)
                    ->orderBy('id', 'ASC');
            if($level != ''){
                $list->where('level', $level);
            }
            if($parent_id != ''){
                $list->where('parent_id', $parent_id);
            }
            if($key != ''){
                $list->where('name','like','%'.$key.'%');
            }
            if($num != ''){
                $list = $list->skip(0)->take($num)->get();
            } else {
                $list = $list->get();
            }
            
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['list' => $list]);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/query_level", 'input' =>Input::all() ,"output" => $response));
        
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-4-9 上午9:50
     * @Description:     采购询价    [ 查询商品报价 ]
     * @param string $token             用户token
     * @param int $level_1              一级分类
     * @param int $level_2              二级分类
     * @param int $level_3              三级分类
     * @param int $status               耗材状态 [1:正常;0:关闭;]
     * @param int $type                 耗材类型 [1:非标准耗材;0:标准耗材;]
     * @param string $volume_size       计量数量
     * @param int $unit_id              计量单位id
     * @param string $brand_name        耗材品牌信息
     * @param string $series_name       耗材系列信息
     * @param string $code              耗材商品编码
     * @param int $sale_unit_id         商品单位id
     * @param string $retrieval_code    商品信息
     * @param string $supplier_name     供应商名称
     * @param int $purchase_status      供应商状态 [0:关闭;1:正常;]
     * @param int $regular_pay          是否定期结算 [0:否;1:是; 现金结算]
     * @param int $sort                 报价排序 [1含税升, 2含税降, 3无税升, 4 无税降]
     * @param int $model_id             车辆车型id
     * @param int $page                 页码 [ 默认为1 ]
     * @param int $page_size            每页数据条数 [ 默认为20 ]
     * @link http://api.anhui.com/v1/scs/purchase/query_price?token=41a72e248b6e157ccecddac59d7a33a3&level_1=1
     * 已经废弃     18-6-5 上午11:31
     */
    public function query_price(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'page' => 'integer',
                'page_size' => 'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $page = Input::get('page', 1);
            $page_size = Input::get('page_size', 20);
            $level_1 = Input::get('level_1', '');           //一级分类
            $level_2 = Input::get('level_2', '');           //二级分类
            $level_3 = Input::get('level_3', '');           //三级分类
            $status = Input::get('status', '');             //耗材状态
            $type = Input::get('type', '');                 //耗材类型
            $volume_size = Input::get('volume_size', '');   //计量数量
            $unit_id = Input::get('unit_id', '');           //计量单位
            $brand_name = Input::get('brand_name', '');     //品牌信息
            $series_name = Input::get('series_name', '');   //系列信息
            $code = Input::get('code', '');                 //商品编码
            $sale_unit_id = Input::get('sale_unit_id', ''); //商品单位
            $retrieval_code = Input::get('retrieval_code', '');//商品信息
            $supplier_name = Input::get('supplier_name', '');  //供应商名称
            $purchase_status = Input::get('purchase_status', '');//供应商状态 0 关闭 1正常
            $regular_pay = Input::get('regular_pay', '');    //是否定期结算 1是 0否（现金结算）
            $sort = Input::get('sort', '');                  //报价排序 1含税升,  2含税降,  3无税升,  4 无税降
            $model_id = Input::get('model_id', '');          //车辆车型
            
            //根据1,2,3级分类 查找三级分类. 验证是否有三级分类，没有第三级分类，直接返回空
            $level_arr = [];    //耗材三级分类id数组
            if($level_1 != '' || $level_2 != '' || $level_3 != ''){
                $level = SupplyClassification::select('supply_classification.id', 'supply_classification.name')
                        ->leftJoin('supply_classification AS lev2', 'supply_classification.parent_id', '=', 'lev2.id')
                        ->leftJoin('supply_classification AS lev1', 'lev2.parent_id', '=', 'lev1.id');
                if($level_3 != ''){
                    $level->where('supply_classification.level', 3);
                    $level->where('supply_classification.id', $level_3);
                }
                if($level_2 != ''){
                    $level->where('lev2.level', 2);
                    $level->where('lev2.id', $level_2);
                }
                if($level_1 != ''){
                    $level->where('lev1.level', 1);
                    $level->where('lev1.id', $level_1);
                }
                $level = $level->orderBy('supply_classification.id')->get();
                if(!empty($level)){
                    foreach ($level as $v) {
                        $level_arr[] = $v->id;
                    }
                }
            }
            
            //验证车型是否有对应的耗材,车型没有对应的耗材，直接返回空
            $dpi_arr = [];
            if ($model_id != '') {
                $dpi = DataSupplyProductItem::select('supply_item_id')
                        ->where('status', 1)
                        ->where('car_model_id', $model_id)
                        ->groupBy('supply_item_id')
                        ->get();
                if (!empty($dpi)) {
                    foreach ($dpi as $v) {
                        $dpi_arr[] = $v->supply_item_id;
                    }
                }
            }
            
            $search_arr = ['si.id as si_id','si.brand_id','si.brand_name','si.model','si.status','si.type','si.name',
                'supplier_goods_price.id','supplier_goods_price.tax_price','supplier_goods_price.no_tax_price',
                'ms.id as suppler_id','ms.status as suppler_status','ms.purchase_status','ms.name as suppler_name',
                'ms.regular_pay'];
            $list = SupplierGoodsPrice::leftJoin('supply_item as si','supplier_goods_price.goods_id','=','si.id')
                    ->leftJoin('maintenance_supplier as ms','supplier_goods_price.supplier_id','=','ms.id')
                    ->select($search_arr)
                    ->where('supplier_goods_price.status',1);
            if($retrieval_code != ''){
                $list->where('si.retrieval_code','like','%'.$retrieval_code.'%');
            }
            if($status != ''){
                $list->where('si.status',$status);
            }
            if($type != ''){
                $list->where('si.type',$type);
            }
            if($volume_size != ''){
                $list->where('si.volume_size','like','%'.$volume_size.'%');
            }
            if($unit_id != ''){
                $list->where('si.unit_id',$unit_id);
            }
            if($brand_name != ''){
                $list->where('si.retrieval_code','like','%'.$brand_name.'%');
            }
            if($series_name != ''){
                $list->where('si.retrieval_code','like','%'.$series_name.'%');
            }
            if($code != ''){
                $list->where('si.retrieval_code','like','%'.$code.'%');
            }
            if($sale_unit_id != ''){
                $list->where('si.sale_unit_id',$sale_unit_id);
            }
            if(!empty($dpi_arr)){
                $list->whereIn('si.id',$dpi_arr); //车型对应的耗材
            }
            if(!empty($level_arr)){
                $list->whereIn('supply_classification_id',$level_arr);  //基础分类
            }
            if($supplier_name != ''){
                $list->where('ms.name','like','%'.$supplier_name.'%');//供应商名称
            }
            if($purchase_status != ''){
                $list->where('ms.purchase_status',$purchase_status);
            }
            if($regular_pay != ''){
                $list->where('regular_pay',$regular_pay);
            }
            $list->orderBy('si.id','ASC');
            switch ($sort){
                case 1:  $list->orderBy('tax_price','ASC');
                    break;
                case 2:  $list->orderBy('tax_price','DESC');
                    break;
                case 3:  $list->orderBy('no_tax_price','ASC');
                    break;
                case 4:  $list->orderBy('no_tax_price','DESC');
                    break;
                default : //默认含税升序
                    $list->orderBy('tax_price','ASC');
            }
            
            $count = $list->count();
            $list = $list->skip($page_size*($page-1))->take($page_size)->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['count' => $count, 'list' => $list]);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/query_price", 'input' =>Input::all() ,"output" => $response));
        
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-4-21 下午12:10
     * @Description:     采购询价    [ 查询商品报价 ]   新的
     * @param string $token             用户token
     * @param string $name              商品信息
     * @param int $level_1              一级分类
     * @param int $level_2              二级分类
     * @param int $level_3              三级分类
     * @param int $supplier_type        供应商类型 1:直营店(门店);2:商户(经销商平台、康众平台);
     * @param int $type                 耗材类型 [1:非标准耗材;0:标准耗材;]
     * @param int $status               耗材状态 [1:正常;0:关闭;]
     * @param string $volume_size       计量数量
     * @param int $unit_id              计量单位id
     * @param string $brand_name        耗材品牌信息
     * @param string $series_name       耗材系列信息
     * @param string $code              耗材商品编码
     * @param int $sale_unit_id         商品单位id
     * @param string $supplier_name     供应商名称
     * @param int $surplus_type         是否有货(是否有库存)  1:有;2:无;
     * @param int $regular_pay          是否定期结算 [0:否;1:是; 现金结算]
     * @param int $sort                 报价排序 [1含税升, 2含税降]
     * @param int $model_id             车辆车型id
     * @param int $purchase_status      供应商状态 0 关闭 1正常 （默认为1正常）
     * @link http://api.anhui.com/v1/scs/purchase/query_price_new?token=e17aa63fb641b26044f7a025960a5570&purchase_status=1&sort=1&status=1&surplus_type=1
     */
    public function query_price_new(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token = Input::get('token');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if(is_null($sc_user)){
                break;
            }
  
            $num = Input::get('num', 1000);    //查询出耗材id的数量
            
            $name = Input::get('name');     //商品信息
            //retrieval_code    耗材的检索码
            $level_1 = Input::get('level_1');           //一级分类
            $level_2 = Input::get('level_2');           //二级分类
            $level_3 = Input::get('level_3');           //三级分类
            $supplier_type = Input::get('supplier_type');   //供应商类型 1:直营店(门店);2:商户(经销商平台、康众平台);
            $type = Input::get('type');                 //耗材类型  [1:非标;0:标准;]
            $status = Input::get('status');             //耗材状态  [1:正常;0:关闭;]
            $volume_size = Input::get('volume_size');   //计量数量
            $unit_id = Input::get('unit_id');           //计量单位
            $brand_name = Input::get('brand_name');     //品牌信息
            $series_name = Input::get('series_name');   //系列信息
            $code = Input::get('code');                 //商品编码
            $sale_unit_id = Input::get('sale_unit_id'); //商品单位
            $supplier_name = trim(Input::get('supplier_name'));  //供应商名称
            $surplus_type = Input::get('surplus_type');  //是否有货(是否有库存)  1:有;2:无;
            $regular_pay = Input::get('regular_pay');    //是否定期结算 1是 0否（现金结算）
            $sort = Input::get('sort');                  //报价排序 1含税升,  2含税降
            $model_id = Input::get('model_id');          //车辆车型
            $purchase_status = Input::get('purchase_status');//供应商状态 0 关闭 1正常 （默认为1正常）
            
            //根据1,2,3级分类 查找三级分类. 验证是否有三级分类，没有第三级分类，直接返回空
            $level_arr = [];    //耗材三级分类id数组
            if($level_1 != '' || $level_2 != '' || $level_3 != ''){
                $level = SupplyClassification::select('supply_classification.id', 'supply_classification.name')
                        ->leftJoin('supply_classification AS lev2', 'supply_classification.parent_id', '=', 'lev2.id')
                        ->leftJoin('supply_classification AS lev1', 'lev2.parent_id', '=', 'lev1.id');
                if($level_3 != ''){
                    $level->where('supply_classification.level', 3);
                    $level->where('supply_classification.id', $level_3);
                }
                if($level_2 != ''){
                    $level->where('lev2.level', 2);
                    $level->where('lev2.id', $level_2);
                }
                if($level_1 != ''){
                    $level->where('lev1.level', 1);
                    $level->where('lev1.id', $level_1);
                }
                $level = $level->orderBy('supply_classification.id')->get();
                if(!empty($level)){
                    foreach ($level as $v) {
                        $level_arr[] = $v->id;
                    }
                }
            }
            //      获取有效的 耗材id、耗材erp_id  start
            $supply_item_id = [];       //耗材id数组
            $supply_item_erp_id = [];   //耗材erp_id数组
            $supply_item_info = [];     //查询出来的耗材详细信息    erp id 作为key
            //根据车型 查询出所对应耗材的 耗材id、耗材erp_id
            if ($model_id != '') {
                $select_str = 'anhui.supply_item.id, anhui.supply_item.brand_id, anhui.supply_item.brand_name, '
                        . 'anhui.supply_item.model, anhui.supply_item.status, anhui.supply_item.type, '
                        . 'anhui.supply_item.name, anhui.supply_item.erp_id';
                $spi_arr = DB::table('anhui_data.supply_product_item')
                        ->leftJoin('anhui.supply_item', 'supply_product_item.supply_item_id', '=', 'anhui.supply_item.erp_id')
                        ->select(DB::raw($select_str))
                        ->where('anhui_data.supply_product_item.status', 1)
                        ->where('anhui_data.supply_product_item.car_model_id', $model_id)
                        ->groupBy('anhui_data.supply_product_item.supply_item_id')
                        ->skip(0)->take($num)->get();
            } else {
                $spi_arr = [];
            }
            if(!empty($spi_arr)){
                foreach ($spi_arr as $v) {
                    $supply_item_id[] = $v->id;
                    $supply_item_erp_id[] = $v->erp_id;
                    $supply_item_info[$v->erp_id]['id'] = $v->id;
                    $supply_item_info[$v->erp_id]['brand_id'] = $v->brand_id;
                    $supply_item_info[$v->erp_id]['brand_name'] = $v->brand_name;
                    $supply_item_info[$v->erp_id]['model'] = $v->model;
                    $supply_item_info[$v->erp_id]['status'] = $v->status;
                    $supply_item_info[$v->erp_id]['type'] = $v->type;
                    $supply_item_info[$v->erp_id]['name'] = $v->name;
                }
            }
            
            //根据搜索条件查询出符合条件的 耗材id、耗材erp_id
            $item_list = DB::table('supply_item')->select('id', 'brand_id', 'brand_name', 'model', 'status', 'type', 'name', 'retrieval_code', 'erp_id');
            if($name != ''){  //商品信息
                $item_list->where('retrieval_code', 'like', '%'.$name.'%');
            }
            if(!empty($level_arr)){ //三级分类
                $item_list->whereIn('supply_classification_id', $level_arr);  //基础分类
            }
            if($type != ''){   //耗材类型
                $item_list->where('type',$type);
            }
            if($status != ''){   //耗材状态
                $item_list->where('status',$status);
            }
            if($volume_size != ''){ //计量数量
                $item_list->where('volume_size', 'like', '%'.$volume_size.'%');
            }
            if($unit_id != ''){ //计量单位
                $item_list->where('unit_id', $unit_id);
            }
            if($brand_name != ''){ //品牌信息
                $item_list->where('retrieval_code', 'like', '%'.$brand_name.'%');
            }
            if($series_name != ''){ //系列信息
                $item_list->where('retrieval_code', 'like', '%'.$series_name.'%');
            }
            if($code != ''){ //商品编码
                $item_list->where('retrieval_code', 'like', '%'.$code.'%');
            }
            if($sale_unit_id != ''){    //商品单位
                $item_list->where('sale_unit_id', $sale_unit_id);
            }
            $item_list = $item_list->skip(0)->take($num)->get();
            
            if(!empty($item_list)){
                foreach ($item_list as $v) {
                    $supply_item_id[] = $v->id;
                    $supply_item_erp_id[] = $v->erp_id;
                    $supply_item_info[$v->erp_id]['id'] = $v->id;
                    $supply_item_info[$v->erp_id]['brand_id'] = $v->brand_id;
                    $supply_item_info[$v->erp_id]['brand_name'] = $v->brand_name;
                    $supply_item_info[$v->erp_id]['model'] = $v->model;
                    $supply_item_info[$v->erp_id]['status'] = $v->status;
                    $supply_item_info[$v->erp_id]['type'] = $v->type;
                    $supply_item_info[$v->erp_id]['name'] = $v->name;
                }
            }
            //      获取有效的 耗材id、耗材erp_id  end

            //获取本身的门店id
            $sc_token = ScsUserToken::getToken($token);
            $sc_user = SCUser::find($sc_token->sc_user_id);
            if($sc_token && $sc_user){
                $shop_id = $sc_user->shop_id;
            } else {
                $shop_id = '';
            }
            
            //查询出的门店报价数据
            //supplier_type 供应商类型 1:门店;2:商户 经销商平台、康众平台;
            $sc_list = [];      //门店报价数据数组
            if($supplier_type != 2 && !empty($supply_item_id)){     //耗材id数组不能为空
                //门店询价查询的时候 还需要额外添加的条件 1、不看自己门店相关耗材     
                //->where('maintenance_storage_item.shop_id', '!=', $shop_id)   【pms不需要这些条件】
                $sc_str = 'MAX(maintenance_storage_item.tax_purchase_price) AS tax_purchase_price, '
                        . 'MAX(maintenance_storage_item.no_tax_purchase_price) AS no_tax_purchase_price, '
                        . 'SUM(current_number) AS current_number, supply_item.id AS si_id, supply_item.brand_id, '
                        . 'supply_item.brand_name, supply_item.model, supply_item. status,supply_item.type, supply_item.name, '
                        . 'maintenance_shops.name AS shop_name, 1 AS regular_pay, 1 AS supplier_type';
                $sc_list = MaintenanceStorageItem::leftJoin('maintenance_shops', 'maintenance_storage_item.shop_id', '=', 'maintenance_shops.id')
                        ->leftJoin('supply_item', 'maintenance_storage_item.item_id', '=', 'supply_item.id')
                        ->select(DB::raw($sc_str))
                        ->groupBy('maintenance_storage_item.shop_id')->groupBy('maintenance_storage_item.item_id')
                        ->where('maintenance_storage_item.type', '1')
                        ->where('maintenance_storage_item.status', '1')
                        ->where('maintenance_storage_item.shop_id', '<>', $shop_id)
                        ->where('maintenance_shops.status', '1');
                if(!empty($supply_item_id)){    //匹配 耗材id
                    $sc_list->whereIn('maintenance_storage_item.item_id', $supply_item_id);
                }
                if($supplier_name != ''){ //搜索门店名称
                    $sc_list->where('maintenance_shops.name', 'like', '%'.$supplier_name.'%');
                }
                if($surplus_type == 1){ //门店或者供应商有货
                    $sc_list->where('current_number', '>', '0');
                }elseif ($surplus_type == 2) {  //门店或者供应商无货
                    $sc_list->where('current_number', '<=', '0');
                }
                //$sc_list = $sc_list->skip(0)->take($num)->get()->toArray();      //查询出的门店报价数据
                $sc_list = $sc_list->skip(0)->take(500)->get()->toArray();      //查询出的门店报价数据
            }
            
            $gongyingshang_res = [];    //经销商平台 、康众 平台的应返回的数据
            if($supplier_type != 1){    //查询经销商平台 、康众 平台的报价数据
                //查询出符合条件的供应商id
                $supplier_erp_id = [];  //供货商所对应的erp_id
                $supplier_info = [];     //查询出来的耗材详细信息   erp id 作为key
                $supplier_list = MaintenanceSupplier::select('id', 'erp_id', 'name', 'regular_pay');
                if(!empty($supplier_name)){ //供应商名称
                    $supplier_list->where('name', 'like', '%'.$supplier_name.'%');
                }
                if($regular_pay != ''){   //是否定期结算 1是 0否（现金结算）
                    $supplier_list->where('regular_pay', $regular_pay);
                }
                if($purchase_status != ''){   //供应商状态 0 关闭 1正常 （默认为1正常）
                    $supplier_list->where('purchase_status', $purchase_status);
                }
                $supplier_list = $supplier_list->skip(0)->take($num)->get();
                
                if(!empty($supplier_list)){
                    foreach ($supplier_list as $v) {
                        $supplier_erp_id[] = $v->erp_id;
                        $supplier_info[$v['erp_id']]['id'] = $v->id;
                        $supplier_info[$v['erp_id']]['name'] = $v->name;
                        $supplier_info[$v['erp_id']]['regular_pay'] = $v->regular_pay;
                    }
                    
                }
                
                //通过 经销商平台 、康众平台获取数据
                $gongyingshang_info = GYSQueryPrice::getQueryPriceList($supply_item_erp_id, $supplier_erp_id, $shop_id);
                if(!empty($gongyingshang_info)){
                    foreach ($gongyingshang_info as $k => $v) {
                        if ($surplus_type == 2) {  //门店或者供应商无货
                            if($v['storage'] > 0 || $v['storage'] == -1){
                                unset($gongyingshang_info[$k]);
                                continue;
                            }
                        }
                        if ($type == 1) {  //耗材类型  [1:非标;0:标准;]
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                        
                        $gongyingshang_res[$k]['tax_purchase_price'] = $v['price'];
                        $gongyingshang_res[$k]['no_tax_purchase_price'] = $v['price'];
                        $gongyingshang_res[$k]['current_number'] = $v['storage'];
                        if(isset($supply_item_info[$v['item_id']])){
                            $gongyingshang_res[$k]['si_id'] = $supply_item_info[$v['item_id']]['id'];   //erp id转换成 耗材id
                            $gongyingshang_res[$k]['brand_id'] = $supply_item_info[$v['item_id']]['brand_id'];
                            $gongyingshang_res[$k]['brand_name'] = $supply_item_info[$v['item_id']]['brand_name'];
                            $gongyingshang_res[$k]['model'] = $supply_item_info[$v['item_id']]['model'];
                            $gongyingshang_res[$k]['status'] = $supply_item_info[$v['item_id']]['status'];
                            $gongyingshang_res[$k]['type'] = $supply_item_info[$v['item_id']]['type'];
                            $gongyingshang_res[$k]['name'] = $supply_item_info[$v['item_id']]['name'];    
                        }else{
                            $item_id_info = SupplyItem::where('erp_id', $v['item_id'])->first()->toArray(); //查询具体耗材的信息
                            $gongyingshang_res[$k]['si_id'] = $item_id_info['id'];   //erp id转换成 耗材id
                            $gongyingshang_res[$k]['brand_id'] = $item_id_info['brand_id'];
                            $gongyingshang_res[$k]['brand_name'] = $item_id_info['brand_name'];
                            $gongyingshang_res[$k]['model'] = $item_id_info['model'];
                            $gongyingshang_res[$k]['status'] = $item_id_info['status'];
                            $gongyingshang_res[$k]['type'] = $item_id_info['type'];
                            $gongyingshang_res[$k]['name'] = $item_id_info['name'];
                        }
                        
                        if(isset($supplier_info[$v['suppller_id']])){
                            $gongyingshang_res[$k]['suppller_id'] = $supplier_info[$v['suppller_id']]['id'];    //供应商id  cms的供应商id
                            $gongyingshang_res[$k]['shop_name'] = $supplier_info[$v['suppller_id']]['name'];    //供应商的名称
                            $gongyingshang_res[$k]['regular_pay'] = $supplier_info[$v['suppller_id']]['regular_pay'];//是否定期结算
                        }else{
                            $supplier_id_info = MaintenanceSupplier::where('erp_id', $v['suppller_id'])->first()->toArray(); //查询具体供应商的信息
                            $gongyingshang_res[$k]['suppller_id'] = $supplier_info[$v['suppller_id']]['id'];    //供应商id  cms的供应商id
                            $gongyingshang_res[$k]['shop_name'] = $supplier_id_info['name'];    //供应商的名称
                            $gongyingshang_res[$k]['regular_pay'] = $supplier_id_info['regular_pay'];//是否定期结算
                        }
                        
                        $gongyingshang_res[$k]['supplier_type'] = 2;    //供应商的类型
                    }
                }
            }
            if($regular_pay != '' && $regular_pay == 0){   //是否定期结算 1是 0否（现金结算）
                //由于门店的定期结算都为1 开启， 所以搜索定期结算关闭的时候，门店的相关数据为空
                $all_list = $gongyingshang_res;
            }else{
                //$all_list = array_merge($sc_list, $gongyingshang_res);
                $all_list = array_merge($gongyingshang_res, $sc_list);
            }
            
            //排序处理  start      耗材id顺序（正序）排序 -> 含税升序/含税降序
            $sort_id = [];      //待排序的耗材id
            $sort_price = [];   //待排序的含税报价
            foreach ($all_list as $k => $v) {
                $sort_id[] = $v['si_id'];
                $sort_price[] = $v['tax_purchase_price'];
//                $all_list[$k]['tax_purchase_price'] = Common::upFow($v['tax_purchase_price']);      //价格末尾去除0
//                $all_list[$k]['no_tax_purchase_price'] = Common::upFow($v['no_tax_purchase_price']);
            }               
            switch ($sort) {    //报价排序 1含税升,  2含税降
                case 1:
                    array_multisort($sort_id, $sort_price, $all_list);
                    break;
                case 2:
                    array_multisort($sort_id, $sort_price, SORT_DESC, $all_list);
                    break;
                default:
                    array_multisort($sort_id, $all_list);
                    break;
            }
            //排序处理  end
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $all_list);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/query_price_new", 'input' =>Input::all() ,"output" => $response));
        
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-6-8 下午2:15
     * @Description:     采购询价 => 通过车架号 一级询价
     * @param string $token             用户token
     * @param string $vin               车架号
     * @param int $brand_id             耗材品牌id
     * @param int $series_id            耗材系列id
     * @param int $level_1              一级分类
     * @param int $level_2              二级分类
     * @param int $level_3              三级分类
     * @param int $supplier_id          供应商id
     * @param string $volume_size       计量数量
     * @param int $unit_id              计量单位id
     * @param int $sale_unit_id         商品单位id
     * @param int $sort                 报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
     * @param int $surplus_type         是否有货(是否有库存)  1:有;2:无;
     * 
     * @param int $purchase_status      供应商状态 0 关闭 1正常 （默认为1正常）
     * @link http://api.anhui.com/v1/scs/purchase/queryPriceByVin?token=786fe6e634788669c0bcc63dba31b07a&vin=WBAPC7108AWK21046
     */
    public function queryPriceByVin(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'vin' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token = Input::get('token');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if(is_null($sc_user)){
                break;
            }
            $vin            = Input::get('vin');                //车架号
            $brand_id       = Input::get('brand_id', '');       //耗材品牌id
            $series_id      = Input::get('series_id', '');      //耗材系列id
            $level_1        = Input::get('level_1', '');        //一级分类
            $level_2        = Input::get('level_2', '');        //二级分类
            $level_3        = Input::get('level_3', '');        //三级分类
            $supplier_id    = Input::get('supplier_id', '');    //供应商id
            $volume_size    = Input::get('volume_size', '');    //计量数量
            $unit_id        = Input::get('unit_id', '');        //计量单位
            $sale_unit_id   = Input::get('sale_unit_id', '');   //商品单位
            $surplus_type   = Input::get('surplus_type', '');   //是否有货(是否有库存)  1:有;2:无;
            $sort           = Input::get('sort', 0);            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            
            $purchase_status = Input::get('purchase_status', 1);//供应商状态 0 关闭 1正常 （默认为1正常）      
            $supply_item_id     = [];       //耗材id数组
            $supply_item_erp_id = [];       //耗材erp_id数组
            $supply_item_info   = [];       //查询出来的耗材详细信息    erp id 作为key
            //返回数组
            $result = [
                'list' => [],
                'vin_info' => [],
                'supplier_arr' => [],
                'unit_arr' => [],
                'sale_unit_arr' => [],
            ];
            //通过经销商平台查询相应车架号适配的耗材、及相关车系信息……
            $vin_info_arr = GYSQueryPrice::getVinInfo($vin);
            if(empty($vin_info_arr) || empty($vin_info_arr['item_erp_id'])){
                //经销商平台通过车架号匹配耗材为空时，直接返回
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
                break;
            }
            //根据1,2,3级分类 查找耗材的三级分类id数组
            $level_arr = SupplyClassification::getSupplyClassificationId($level_1, $level_2, $level_3);
            if($level_1 != '' || $level_2 != '' || $level_3 != ''){
                //如果一二三级分类查询条件不为空时， 匹配到的三级分类id为空时，直接返回
                if(empty($level_arr)){
                    $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
                    break;
                }
            }
            //根据车架号、及相应的搜索条件查询出符合条件的 耗材id、耗材erp_id
//            $select_arr = [
//                'id', 'brand_id', 'brand_name', 'series_id', 'series_name', 'model', 'status', 'type', 
//                'unit_id', 'sale_unit_id', 'code', 'name', 'retrieval_code', 'erp_id',
//            ];
            $select_arr = [
                'supply_item.id', 'supply_item.brand_id', 'supply_item.series_id', 'supply_item.model', 'supply_item.status', 
                'supply_item.type', 'supply_item.unit_id', 'supply_item.sale_unit_id', 'supply_item.code', 'supply_item.name', 
                'supply_item.retrieval_code', 'supply_item.erp_id',
                'supply_brand.brand_name', 'supply_series.name AS series_name',
            ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
            $item_list = SupplyItem::leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                    ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                    ->select($select_arr)
                    ->whereIn('supply_item.erp_id', $vin_info_arr['item_erp_id']);
            if(!empty($level_arr)){ //三级分类
                $item_list->whereIn('supply_item.supply_classification_id', $level_arr);  //基础分类
            }
            if($brand_id != ''){    //耗材品牌
                $item_list->where('supply_item.brand_id', $brand_id);
            }
            if($series_id != ''){   //耗材系列
                $item_list->where('supply_item.series_id', $series_id);
            }
            if($volume_size != ''){ //计量数量
                $item_list->where('supply_item.volume_size', 'like', '%'.$volume_size.'%');
            }
            if($unit_id != ''){ //计量单位
                $item_list->where('supply_item.unit_id', $unit_id);
            }
            if($sale_unit_id != ''){    //商品单位
                $item_list->where('supply_item.sale_unit_id', $sale_unit_id);
            }
            $item_list = $item_list->get();
            if(!empty($item_list)){
                foreach ($item_list as $v) {
                    $supply_item_id[] = $v->id;
                    if($v->type == 0){  //0:标准;1:非标;
                        $supply_item_erp_id[] = $v->erp_id;
                    }
                    $supply_item_info[$v->erp_id] = $v->toArray();
                }
            }
            $shop_id = $sc_user->shop_id;       //获取本身的门店id
            
            //查询出的门店报价数据
            $sc_list = [];                      //门店报价数据数组
            if(!empty($supply_item_id)){        //耗材id数组不能为空
                //门店询价查询的时候 还需要额外添加的条件 1、不看自己门店相关耗材
                $sc_str = 'MAX(maintenance_storage_item.tax_purchase_price) AS tax_purchase_price, '
                        . 'MAX(maintenance_storage_item.no_tax_purchase_price) AS no_tax_purchase_price, '
                        . 'SUM(current_number) AS current_number, supply_item.id AS si_id, supply_item.brand_id, '
                        . 'supply_item.series_id, supply_item.model, '
                        . 'supply_item.unit_id, supply_item.sale_unit_id, '
                        . 'supply_item.status, supply_item.type, supply_item.code, supply_item.name, '
                        . 'maintenance_shops.id AS supplier_id, maintenance_shops.name AS shop_name, 1 AS regular_pay, 1 AS supplier_type, '
                        . 'supply_brand.brand_name, supply_series.name AS series_name ';
                $sc_list = MaintenanceStorageItem::leftJoin('maintenance_shops', 'maintenance_storage_item.shop_id', '=', 'maintenance_shops.id')
                        ->leftJoin('supply_item', 'maintenance_storage_item.item_id', '=', 'supply_item.id')
                        ->leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                        ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                        ->select(DB::raw($sc_str))
                        ->groupBy('maintenance_storage_item.shop_id')->groupBy('maintenance_storage_item.item_id')
                        ->where('maintenance_storage_item.type', '1')
                        ->where('maintenance_storage_item.status', '1')
                        ->where('maintenance_storage_item.shop_id', '<>', $shop_id)
                        ->whereIn('maintenance_storage_item.item_id', $supply_item_id)
                        ->where('maintenance_shops.status', '1');
                if($supplier_id != ''){     //匹配门店id
                    $sc_list->where('maintenance_shops.id', $supplier_id);
                }
                if($surplus_type == 1){         //门店或者供应商有货
                    $sc_list->where('current_number', '>', '0');
                }elseif ($surplus_type == 2) {  //门店或者供应商无货
                    $sc_list->where('current_number', '<=', '0');
                }
                $sc_list = $sc_list->get()->toArray();      //查询出的门店报价数据
            }
            
            //查询经销商平台 、康众 平台的报价数据  start
            //查询出符合条件的供应商id
            $gongyingshang_res  = [];  //经销商平台 、康众 平台的应返回的数据
            $supplier_erp_id    = [];  //供货商所对应的erp_id
            $supplier_info      = [];  //查询出来的耗材详细信息   erp id 作为key
            $supplier_list = MaintenanceSupplier::select('id', 'erp_id', 'name', 'regular_pay');
            if($supplier_id != ''){     //匹配具体供货商
                $supplier_list->where('id', $supplier_id);
            }
            if($purchase_status != ''){   //供应商状态 0 关闭 1正常 （默认为1正常）
                $supplier_list->where('purchase_status', $purchase_status);
            }
            $supplier_list = $supplier_list->get();
            
            if(!empty($supplier_list)){
                foreach ($supplier_list as $v) {
                    $supplier_erp_id[] = $v->erp_id;
                    $supplier_info[$v['erp_id']] = $v->toArray();
                }
            }
            
            //通过 经销商平台 、康众平台获取数据
            $gongyingshang_info = GYSQueryPrice::getQueryPriceList($supply_item_erp_id, $supplier_erp_id, $shop_id);
            if(!empty($gongyingshang_info)){
                foreach ($gongyingshang_info as $k => $v) {
                    //是否有货(是否有库存)  1:有;2:无;
                    if($surplus_type == 1){
                        if($v['storage'] <= 0 && $v['storage'] != -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }elseif ($surplus_type == 2) {
                        if($v['storage'] > 0 || $v['storage'] == -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }
                    $gongyingshang_res[$k]['tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['no_tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['current_number'] = $v['storage'];
                    if(isset($supply_item_info[$v['item_id']])){
                        $gongyingshang_res[$k]['si_id'] = $supply_item_info[$v['item_id']]['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $supply_item_info[$v['item_id']]['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $supply_item_info[$v['item_id']]['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $supply_item_info[$v['item_id']]['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $supply_item_info[$v['item_id']]['series_name'];
                        $gongyingshang_res[$k]['model'] = $supply_item_info[$v['item_id']]['model'];
                        $gongyingshang_res[$k]['status'] = $supply_item_info[$v['item_id']]['status'];
                        $gongyingshang_res[$k]['type'] = $supply_item_info[$v['item_id']]['type'];
                        $gongyingshang_res[$k]['code'] = $supply_item_info[$v['item_id']]['code'];    
                        $gongyingshang_res[$k]['name'] = $supply_item_info[$v['item_id']]['name'];    
                        $gongyingshang_res[$k]['unit_id'] = $supply_item_info[$v['item_id']]['unit_id'];    
                        $gongyingshang_res[$k]['sale_unit_id'] = $supply_item_info[$v['item_id']]['sale_unit_id'];    
                    }else{
                        $select_arr = [
                            'supply_item.id', 'supply_item.brand_id', 'supply_item.series_id', 'supply_item.model', 'supply_item.status', 
                            'supply_item.type', 'supply_item.unit_id', 'supply_item.sale_unit_id', 'supply_item.code', 'supply_item.name', 
                            'supply_item.retrieval_code', 'supply_item.erp_id',
                            'supply_brand.brand_name', 'supply_series.name AS series_name',
                        ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
                        $item_id_info = SupplyItem::leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                                ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                                ->where('erp_id', $v['item_id'])
                                ->select($select_arr)->first()->toArray(); //查询具体耗材的信息
                        $gongyingshang_res[$k]['si_id'] = $item_id_info['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $item_id_info['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $item_id_info['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $item_id_info['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $item_id_info['series_name'];
                        $gongyingshang_res[$k]['model'] = $item_id_info['model'];
                        $gongyingshang_res[$k]['status'] = $item_id_info['status'];
                        $gongyingshang_res[$k]['type'] = $item_id_info['type'];
                        $gongyingshang_res[$k]['code'] = $item_id_info['code'];
                        $gongyingshang_res[$k]['name'] = $item_id_info['name'];
                        $gongyingshang_res[$k]['unit_id'] = $item_id_info['unit_id'];
                        $gongyingshang_res[$k]['sale_unit_id'] = $item_id_info['sale_unit_id'];
                    }
                    if(isset($supplier_info[$v['suppller_id']])){
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_info[$v['suppller_id']]['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_info[$v['suppller_id']]['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_info[$v['suppller_id']]['regular_pay'];//是否定期结算
                    }else{
                        $supplier_id_info = MaintenanceSupplier::where('erp_id', $v['suppller_id'])->first()->toArray(); //查询具体供应商的信息
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_id_info['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_id_info['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_id_info['regular_pay'];//是否定期结算
                    }
                    $gongyingshang_res[$k]['supplier_type'] = 2;    //供应商的类型
                }
            }
            //查询经销商平台 、康众 平台的报价数据 end
            
            $unit_list = DataSupplyUnit::getSupplyUnitList(0, 2);  //获取耗材单位数组
            //$all_list = array_merge($sc_list, $gongyingshang_res);
            $all_list = array_merge($gongyingshang_res, $sc_list);
            $supplier_arr   = [];           //供应商数组
            $unit_arr       = [];           //计量单位数组
            $sale_unit_arr  = [];           //商品单位数组
            $price_sort     = [];           //待排序的含税报价
            $storage_sort   = [];           //待排序的库存
            foreach ($all_list as $v) {
                $temp_1 = [
                    'supplier_id' => $v['supplier_id'],
                    'shop_name' => $v['shop_name'],
                ];
                $supplier_arr[$v['supplier_id']] = $temp_1;
                $temp_2 = [
                    'unit_id' => $v['unit_id'],
                    'unit_name' => $unit_list[$v['unit_id']],
                ];
                $unit_arr[$v['unit_id']] = $temp_2;
                $temp_3 = [
                    'sale_unit_id' => $v['sale_unit_id'],
                    'sale_unit_name' => $unit_list[$v['sale_unit_id']],
                ];
                $sale_unit_arr[$v['sale_unit_id']] = $temp_3;
                $price_sort[] = $v['tax_purchase_price'];
                $storage_sort[] = $v['current_number'];
            }
            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            switch ($sort) {
                case 1:
                    array_multisort($price_sort, $all_list);
                    break;
                case 2:
                    array_multisort($price_sort, SORT_DESC, $all_list);
                    break;
                case 3:
                    array_multisort($storage_sort, $all_list);
                    break;
                case 4:
                    array_multisort($storage_sort, SORT_DESC, $all_list);
                    break;
                default:
                    break;
            }
            
            $result = [
                'list' => $all_list,
                'vin_info' => $vin_info_arr['vin_info'],
                'supplier_arr' => array_values($supplier_arr),
                'unit_arr' => array_values($unit_arr),
                'sale_unit_arr' => array_values($sale_unit_arr),
            ];
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/queryPriceByVin", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-6-6 下午1:50
     * @Description:     采购询价 => 通过车型 一级询价
     * @param string $token             用户token
     * @param int $model_id             车辆车型id
     * @param int $brand_id             耗材品牌id
     * @param int $series_id            耗材系列id
     * @param int $level_1              一级分类
     * @param int $level_2              二级分类
     * @param int $level_3              三级分类
     * @param int $supplier_id          供应商id
     * @param string $volume_size       计量数量
     * @param int $unit_id              计量单位id
     * @param int $sale_unit_id         商品单位id
     * @param int $surplus_type         是否有货(是否有库存)  1:有;2:无;
     * @param int $sort                 报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
     * 
     * @param int $purchase_status      供应商状态 0 关闭 1正常 （默认为1正常）
     * @link http://api.anhui.com/v1/scs/purchase/queryPriceByModel?token=34957948583f6a2e2aae3bca5b54b8c1&model_id=3925
     */
    public function queryPriceByModel(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'model_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token = Input::get('token');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if(is_null($sc_user)){
                break;
            }
            $model_id       = Input::get('model_id');               //车辆车型id
            $brand_id       = Input::get('brand_id', '');           //耗材品牌id
            $series_id      = Input::get('series_id', '');          //耗材系列id
            $level_1        = Input::get('level_1', '');            //一级分类
            $level_2        = Input::get('level_2', '');            //二级分类
            $level_3        = Input::get('level_3', '');            //三级分类
            $supplier_id    = Input::get('supplier_id', '');        //供应商id
            $volume_size    = Input::get('volume_size', '');        //计量数量
            $unit_id        = Input::get('unit_id', '');            //计量单位
            $sale_unit_id   = Input::get('sale_unit_id', '');       //商品单位
            $surplus_type   = Input::get('surplus_type', '');       //是否有货(是否有库存)  1:有;2:无;
            $sort           = Input::get('sort', 0);            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            
            $purchase_status = Input::get('purchase_status', 1);//供应商状态 0 关闭 1正常 （默认为1正常）      
            $supply_item_id     = [];       //耗材id数组
            $supply_item_erp_id = [];       //耗材erp_id数组
            $supply_item_info   = [];       //查询出来的耗材详细信息    erp id 作为key
            //返回数组
            $result = [
                'list' => [],
                'vin_info' => [],
                'supplier_arr' => [],
                'unit_arr' => [],
                'sale_unit_arr' => [],
            ];
            //根据1,2,3级分类 查找耗材的三级分类id数组
            $level_arr = SupplyClassification::getSupplyClassificationId($level_1, $level_2, $level_3);
            if($level_1 != '' || $level_2 != '' || $level_3 != ''){
                //如果一二三级分类查询条件不为空时， 匹配到的三级分类id为空时，直接返回
                if(empty($level_arr)){
                    $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
                    break;
                }
            }
            
            //根据车型id、及相应的搜索条件查询出符合条件的 耗材id、耗材erp_id
            $select_arr = [
                'anhui.supply_item.id', 'anhui.supply_item.brand_id', 'anhui.supply_item.series_id', 
                'anhui.supply_item.model', 'anhui.supply_item.status', 'anhui.supply_item.type', 
                'anhui.supply_item.unit_id', 'anhui.supply_item.sale_unit_id',
                'anhui.supply_item.code', 'anhui.supply_item.name', 'anhui.supply_item.retrieval_code', 'anhui.supply_item.erp_id',
                'anhui_data.supply_brand.brand_name', 'anhui_data.supply_series.name AS series_name',
            ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
            $item_list = DataSupplyProductItem::leftJoin('anhui.supply_item', 'supply_product_item.supply_item_id', '=', 'anhui.supply_item.erp_id')
                    ->leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                    ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                    ->select($select_arr)
                    ->where('anhui_data.supply_product_item.status', 1)
                    ->where('anhui_data.supply_product_item.car_model_id', $model_id)
                    ->where('anhui.supply_item.status', 1);     //耗材状态 0:关闭;1:正常;
            
            if(!empty($level_arr)){ //三级分类
                $item_list->whereIn('anhui.supply_item.supply_classification_id', $level_arr);  //基础分类
            }
            if($brand_id != ''){    //耗材品牌
                $item_list->where('anhui.supply_item.brand_id', $brand_id);
            }
            if($series_id != ''){   //耗材系列
                $item_list->where('anhui.supply_item.series_id', $series_id);
            }
            if($volume_size != ''){ //计量数量
                $item_list->where('anhui.supply_item.volume_size', 'like', '%'.$volume_size.'%');
            }
            if($unit_id != ''){ //计量单位
                $item_list->where('anhui.supply_item.unit_id', $unit_id);
            }
            if($sale_unit_id != ''){    //商品单位
                $item_list->where('anhui.supply_item.sale_unit_id', $sale_unit_id);
            }
            $item_list = $item_list->get();
            
            if(!empty($item_list)){
                foreach ($item_list as $v) {
                    $supply_item_id[] = $v->id;
                    if($v->type == 0){  //0:标准;1:非标;
                        $supply_item_erp_id[] = $v->erp_id;
                    }
                    $supply_item_info[$v->erp_id] = $v->toArray();
                }
            }
            $shop_id = $sc_user->shop_id;       //获取本身的门店id
            
            //查询出的门店报价数据
            $sc_list = [];                      //门店报价数据数组
            if(!empty($supply_item_id)){        //耗材id数组不能为空
                //门店询价查询的时候 还需要额外添加的条件 1、不看自己门店相关耗材
                $sc_str = 'MAX(maintenance_storage_item.tax_purchase_price) AS tax_purchase_price, '
                        . 'MAX(maintenance_storage_item.no_tax_purchase_price) AS no_tax_purchase_price, '
                        . 'SUM(current_number) AS current_number, supply_item.id AS si_id, supply_item.brand_id, '
                        . 'supply_item.series_id, supply_item.model, '
                        . 'supply_item.unit_id, supply_item.sale_unit_id, '
                        . 'supply_item.status, supply_item.type, supply_item.code, supply_item.name, '
                        . 'maintenance_shops.id AS supplier_id, maintenance_shops.name AS shop_name, 1 AS regular_pay, 1 AS supplier_type, '
                        . 'supply_brand.brand_name, supply_series.name AS series_name';
                $sc_list = MaintenanceStorageItem::leftJoin('maintenance_shops', 'maintenance_storage_item.shop_id', '=', 'maintenance_shops.id')
                        ->leftJoin('supply_item', 'maintenance_storage_item.item_id', '=', 'supply_item.id')
                        ->leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                        ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                        ->select(DB::raw($sc_str))
                        ->groupBy('maintenance_storage_item.shop_id')->groupBy('maintenance_storage_item.item_id')
                        ->where('maintenance_storage_item.type', '1')
                        ->where('maintenance_storage_item.status', '1')
                        ->where('maintenance_storage_item.shop_id', '<>', $shop_id)
                        ->whereIn('maintenance_storage_item.item_id', $supply_item_id)
                        ->where('maintenance_shops.status', '1');
                if($supplier_id != ''){     //匹配门店id
                    $sc_list->where('maintenance_shops.id', $supplier_id);
                }
                if($surplus_type == 1){         //门店或者供应商有货
                    $sc_list->where('current_number', '>', '0');
                }elseif ($surplus_type == 2) {  //门店或者供应商无货
                    $sc_list->where('current_number', '<=', '0');
                }
                $sc_list = $sc_list->get()->toArray();      //查询出的门店报价数据
            }
            
            //查询经销商平台 、康众 平台的报价数据  start
            //查询出符合条件的供应商id
            $gongyingshang_res  = [];       //经销商平台 、康众 平台的应返回的数据
            $supplier_erp_id    = [];       //供货商所对应的erp_id
            $supplier_info      = [];       //查询出来的耗材详细信息   erp id 作为key
            $supplier_list = MaintenanceSupplier::select('id', 'erp_id', 'name', 'regular_pay');
            if($supplier_id != ''){     //匹配具体供货商
                $supplier_list->where('id', $supplier_id);
            }
            if($purchase_status != ''){   //供应商状态 0 关闭 1正常 （默认为1正常）
                $supplier_list->where('purchase_status', $purchase_status);
            }
            $supplier_list = $supplier_list->get();
            
            if(!empty($supplier_list)){
                foreach ($supplier_list as $v) {
                    $supplier_erp_id[] = $v->erp_id;
                    $supplier_info[$v['erp_id']] = $v->toArray();
                }
            }
            //通过 经销商平台 、康众平台获取数据
            $gongyingshang_info = GYSQueryPrice::getQueryPriceList($supply_item_erp_id, $supplier_erp_id, $shop_id);
            if(!empty($gongyingshang_info)){
                foreach ($gongyingshang_info as $k => $v) {
                    //是否有货(是否有库存)  1:有;2:无;
                    if($surplus_type == 1){
                        if($v['storage'] <= 0 && $v['storage'] != -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }elseif ($surplus_type == 2) {
                        if($v['storage'] > 0 || $v['storage'] == -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }
                    $gongyingshang_res[$k]['tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['no_tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['current_number'] = $v['storage'];
                    if(isset($supply_item_info[$v['item_id']])){
                        $gongyingshang_res[$k]['si_id'] = $supply_item_info[$v['item_id']]['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $supply_item_info[$v['item_id']]['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $supply_item_info[$v['item_id']]['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $supply_item_info[$v['item_id']]['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $supply_item_info[$v['item_id']]['series_name'];
                        $gongyingshang_res[$k]['model'] = $supply_item_info[$v['item_id']]['model'];
                        $gongyingshang_res[$k]['status'] = $supply_item_info[$v['item_id']]['status'];
                        $gongyingshang_res[$k]['type'] = $supply_item_info[$v['item_id']]['type'];
                        $gongyingshang_res[$k]['code'] = $supply_item_info[$v['item_id']]['code'];    
                        $gongyingshang_res[$k]['name'] = $supply_item_info[$v['item_id']]['name'];    
                        $gongyingshang_res[$k]['unit_id'] = $supply_item_info[$v['item_id']]['unit_id'];    
                        $gongyingshang_res[$k]['sale_unit_id'] = $supply_item_info[$v['item_id']]['sale_unit_id'];    
                    }else{
                        $select_arr = [
                            'supply_item.id', 'supply_item.brand_id', 'supply_item.series_id', 'supply_item.model', 'supply_item.status', 
                            'supply_item.type', 'supply_item.unit_id', 'supply_item.sale_unit_id', 'supply_item.code', 'supply_item.name', 
                            'supply_item.retrieval_code', 'supply_item.erp_id',
                            'supply_brand.brand_name', 'supply_series.name AS series_name',
                        ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
                        $item_id_info = SupplyItem::leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                                ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                                ->where('erp_id', $v['item_id'])
                                ->select($select_arr)->first()->toArray(); //查询具体耗材的信息
                        $gongyingshang_res[$k]['si_id'] = $item_id_info['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $item_id_info['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $item_id_info['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $item_id_info['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $item_id_info['series_name'];
                        $gongyingshang_res[$k]['model'] = $item_id_info['model'];
                        $gongyingshang_res[$k]['status'] = $item_id_info['status'];
                        $gongyingshang_res[$k]['type'] = $item_id_info['type'];
                        $gongyingshang_res[$k]['code'] = $item_id_info['code'];
                        $gongyingshang_res[$k]['name'] = $item_id_info['name'];
                        $gongyingshang_res[$k]['unit_id'] = $item_id_info['unit_id'];
                        $gongyingshang_res[$k]['sale_unit_id'] = $item_id_info['sale_unit_id'];
                    }
                    if(isset($supplier_info[$v['suppller_id']])){
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_info[$v['suppller_id']]['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_info[$v['suppller_id']]['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_info[$v['suppller_id']]['regular_pay'];//是否定期结算
                    }else{
                        $supplier_id_info = MaintenanceSupplier::where('erp_id', $v['suppller_id'])->first()->toArray(); //查询具体供应商的信息
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_id_info['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_id_info['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_id_info['regular_pay'];//是否定期结算
                    }
                    $gongyingshang_res[$k]['supplier_type'] = 2;    //供应商的类型
                }
            }
            //查询经销商平台 、康众 平台的报价数据 end
            
            $unit_list = DataSupplyUnit::getSupplyUnitList(0, 2);  //获取耗材单位数组
            
            //$all_list = array_merge($sc_list, $gongyingshang_res);
            $all_list = array_merge($gongyingshang_res, $sc_list);
            $supplier_arr   = [];           //供应商数组
            $unit_arr       = [];           //计量单位数组
            $sale_unit_arr  = [];           //商品单位数组
            $price_sort     = [];           //待排序的含税报价
            $storage_sort   = [];           //待排序的库存
            foreach ($all_list as $v) {
                $temp_1 = [
                    'supplier_id' => $v['supplier_id'],
                    'shop_name' => $v['shop_name'],
                ];
                $supplier_arr[$v['supplier_id']] = $temp_1;
                $temp_2 = [
                    'unit_id' => $v['unit_id'],
                    'unit_name' => $unit_list[$v['unit_id']],
                ];
                $unit_arr[$v['unit_id']] = $temp_2;
                $temp_3 = [
                    'sale_unit_id' => $v['sale_unit_id'],
                    'sale_unit_name' => $unit_list[$v['sale_unit_id']],
                ];
                $sale_unit_arr[$v['sale_unit_id']] = $temp_3;
                $price_sort[] = $v['tax_purchase_price'];
                $storage_sort[] = $v['current_number'];
            }
            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            switch ($sort) {
                case 1:
                    array_multisort($price_sort, $all_list);
                    break;
                case 2:
                    array_multisort($price_sort, SORT_DESC, $all_list);
                    break;
                case 3:
                    array_multisort($storage_sort, $all_list);
                    break;
                case 4:
                    array_multisort($storage_sort, SORT_DESC, $all_list);
                    break;
                default:
                    break;
            }
            
            $result = [
                'list' => $all_list,
                'supplier_arr' => array_values($supplier_arr),
                'unit_arr' => array_values($unit_arr),
                'sale_unit_arr' => array_values($sale_unit_arr),
            ];
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/queryPriceByModel", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-6-7 下午1:30
     * @Description:     采购询价 => 通过商品名称、商品编码 一级询价
     * @param string $token             用户token
     * @param string $name              商品名称、编码
     * @param int $name_type            搜索类型 [1:商品名称;2:商品编码;]
     * @param int $brand_id             耗材品牌id
     * @param int $series_id            耗材系列id
     * @param int $level_1              一级分类
     * @param int $level_2              二级分类
     * @param int $level_3              三级分类
     * @param int $supplier_id          供应商id
     * @param string $volume_size       计量数量
     * @param int $unit_id              计量单位id
     * @param int $sale_unit_id         商品单位id
     * @param int $surplus_type         是否有货(是否有库存)  1:有;2:无;
     * @param int $sort                 报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
     * 
     * @param int $purchase_status      供应商状态 0 关闭 1正常 （默认为1正常）
     * @link http://api.anhui.com/v1/scs/purchase/queryPriceByName?token=786fe6e634788669c0bcc63dba31b07a&name=博世
     */
    public function queryPriceByName(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'name' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $token = Input::get('token');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if(is_null($sc_user)){
                break;
            }
            $num = Input::get('num', 1500);    //查询出耗材id的数量
            
            $name           = Input::get('name');               //商品名称、编码
            $name_type      = Input::get('name_type', '');      //搜索类型 [1:商品名称;2:商品编码;]
            $brand_id       = Input::get('brand_id', '');       //耗材品牌id
            $series_id      = Input::get('series_id', '');      //耗材系列id
            $level_1        = Input::get('level_1', '');        //一级分类
            $level_2        = Input::get('level_2', '');        //二级分类
            $level_3        = Input::get('level_3', '');        //三级分类
            $supplier_id    = Input::get('supplier_id', '');    //供应商id
            $volume_size    = Input::get('volume_size', '');    //计量数量
            $unit_id        = Input::get('unit_id', '');        //计量单位
            $sale_unit_id   = Input::get('sale_unit_id', '');   //商品单位
            $surplus_type   = Input::get('surplus_type', '');   //是否有货(是否有库存)  1:有;2:无;
            $sort           = Input::get('sort', 0);            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            
            $purchase_status = Input::get('purchase_status', 1);//供应商状态 0 关闭 1正常 （默认为1正常）      
            $supply_item_id     = [];       //耗材id数组
            $supply_item_erp_id = [];       //耗材erp_id数组
            $supply_item_info   = [];       //查询出来的耗材详细信息    erp id 作为key
            //返回数组
            $result = [
                'list' => [],
                'vin_info' => [],
                'supplier_arr' => [],
                'unit_arr' => [],
                'sale_unit_arr' => [],
            ];
            //根据1,2,3级分类 查找耗材的三级分类id数组
            $level_arr = SupplyClassification::getSupplyClassificationId($level_1, $level_2, $level_3);
            if($level_1 != '' || $level_2 != '' || $level_3 != ''){
                //如果一二三级分类查询条件不为空时， 匹配到的三级分类id为空时，直接返回
                if(empty($level_arr)){
                    $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
                    break;
                }
            }
            
            //根据商品名称、及相应的搜索条件查询出符合条件的 耗材id、耗材erp_id
//            $select_arr = [
//                'id', 'brand_id', 'brand_name', 'series_id', 'series_name', 'model', 'status', 'type', 
//                'unit_id', 'sale_unit_id', 'code', 'name', 'retrieval_code', 'erp_id',
//            ];
            $select_arr = [
                'supply_item.id', 'supply_item.brand_id', 'supply_item.series_id', 'supply_item.model', 'supply_item.status', 
                'supply_item.type', 'supply_item.unit_id', 'supply_item.sale_unit_id', 'supply_item.code', 'supply_item.name', 
                'supply_item.retrieval_code', 'supply_item.erp_id',
                'supply_brand.brand_name', 'supply_series.name AS series_name',
            ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
            $item_list = SupplyItem::leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                    ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                    ->select($select_arr);
            //商品名称、编码 [1:商品名称;2:商品编码;]
            if($name_type == 1){
                $item_list->where('supply_item.name', 'like', '%'.$name.'%');
            } else {
                //$item_list->where('supply_item.retrieval_code', 'like', '%'.$name.'%');     //检索检索码
                $item_list->where('supply_item.code', 'like', '%'.$name.'%');     //检索商品编码
            }
            if(!empty($level_arr)){ //三级分类
                $item_list->whereIn('supply_item.supply_classification_id', $level_arr);  //基础分类
            }
            if($brand_id != ''){    //耗材品牌
                $item_list->where('supply_item.brand_id', $brand_id);
            }
            if($series_id != ''){   //耗材系列
                $item_list->where('supply_item.series_id', $series_id);
            }
            if($volume_size != ''){ //计量数量
                $item_list->where('supply_item.volume_size', 'like', '%'.$volume_size.'%');
            }
            if($unit_id != ''){ //计量单位
                $item_list->where('supply_item.unit_id', $unit_id);
            }
            if($sale_unit_id != ''){    //商品单位
                $item_list->where('supply_item.sale_unit_id', $sale_unit_id);
            }
            $item_list = $item_list->skip(0)->take($num)->get();
            //$item_list = $item_list->get();
            if(!empty($item_list)){
                foreach ($item_list as $v) {
                    $supply_item_id[] = $v->id;
                    if($v->type == 0){  //0:标准;1:非标;
                        $supply_item_erp_id[] = $v->erp_id;
                    }
                    $supply_item_info[$v->erp_id] = $v->toArray();
                }
            }
            $shop_id = $sc_user->shop_id;       //获取本身的门店id
            
            //查询出的门店报价数据
            $sc_list = [];                      //门店报价数据数组
            if(!empty($supply_item_id)){        //耗材id数组不能为空
                //门店询价查询的时候 还需要额外添加的条件 1、不看自己门店相关耗材
                $sc_str = 'MAX(maintenance_storage_item.tax_purchase_price) AS tax_purchase_price, '
                        . 'MAX(maintenance_storage_item.no_tax_purchase_price) AS no_tax_purchase_price, '
                        . 'SUM(current_number) AS current_number, supply_item.id AS si_id, supply_item.brand_id, '
                        . 'supply_item.series_id, supply_item.model, '
                        . 'supply_item.unit_id, supply_item.sale_unit_id, '
                        . 'supply_item.status, supply_item.type, supply_item.code, supply_item.name, '
                        . 'maintenance_shops.id AS supplier_id, maintenance_shops.name AS shop_name, 1 AS regular_pay, 1 AS supplier_type, '
                        . 'supply_brand.brand_name, supply_series.name AS series_name';
                $sc_list = MaintenanceStorageItem::leftJoin('maintenance_shops', 'maintenance_storage_item.shop_id', '=', 'maintenance_shops.id')
                        ->leftJoin('supply_item', 'maintenance_storage_item.item_id', '=', 'supply_item.id')
                        ->leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                        ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                        ->select(DB::raw($sc_str))
                        ->groupBy('maintenance_storage_item.shop_id')->groupBy('maintenance_storage_item.item_id')
                        ->where('maintenance_storage_item.type', '1')
                        ->where('maintenance_storage_item.status', '1')
                        ->where('maintenance_storage_item.shop_id', '<>', $shop_id)
                        ->whereIn('maintenance_storage_item.item_id', $supply_item_id)
                        ->where('maintenance_shops.status', '1');
                if($supplier_id != ''){     //匹配门店id
                    $sc_list->where('maintenance_shops.id', $supplier_id);
                }
                if($surplus_type == 1){         //门店或者供应商有货
                    $sc_list->where('current_number', '>', '0');
                }elseif ($surplus_type == 2) {  //门店或者供应商无货
                    $sc_list->where('current_number', '<=', '0');
                }
                $sc_list = $sc_list->get()->toArray();      //查询出的门店报价数据
            }
            
            //查询经销商平台 、康众 平台的报价数据  start
            //查询出符合条件的供应商id
            $gongyingshang_res  = [];  //经销商平台 、康众 平台的应返回的数据
            $supplier_erp_id    = [];  //供货商所对应的erp_id
            $supplier_info      = [];  //查询出来的耗材详细信息   erp id 作为key
            $supplier_list = MaintenanceSupplier::select('id', 'erp_id', 'name', 'regular_pay');
            if($supplier_id != ''){     //匹配具体供货商
                $supplier_list->where('id', $supplier_id);
            }
            if($purchase_status != ''){   //供应商状态 0 关闭 1正常 （默认为1正常）
                $supplier_list->where('purchase_status', $purchase_status);
            }
            //$supplier_list = $supplier_list->get();
            $supplier_list = $supplier_list->skip(0)->take($num)->get();
            
            if(!empty($supplier_list)){
                foreach ($supplier_list as $v) {
                    $supplier_erp_id[] = $v->erp_id;
                    $supplier_info[$v['erp_id']] = $v->toArray();
                }
            }
            
            //通过 经销商平台 、康众平台获取数据
            $gongyingshang_info = GYSQueryPrice::getQueryPriceList($supply_item_erp_id, $supplier_erp_id, $shop_id);
            if(!empty($gongyingshang_info)){
                foreach ($gongyingshang_info as $k => $v) {
                    //是否有货(是否有库存)  1:有;2:无;
                    if($surplus_type == 1){
                        if($v['storage'] <= 0 && $v['storage'] != -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }elseif ($surplus_type == 2) {
                        if($v['storage'] > 0 || $v['storage'] == -1){
                            unset($gongyingshang_info[$k]);
                            continue;
                        }
                    }
                    $gongyingshang_res[$k]['tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['no_tax_purchase_price'] = $v['price'];
                    $gongyingshang_res[$k]['current_number'] = $v['storage'];
                    if(isset($supply_item_info[$v['item_id']])){
                        $gongyingshang_res[$k]['si_id'] = $supply_item_info[$v['item_id']]['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $supply_item_info[$v['item_id']]['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $supply_item_info[$v['item_id']]['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $supply_item_info[$v['item_id']]['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $supply_item_info[$v['item_id']]['series_name'];
                        $gongyingshang_res[$k]['model'] = $supply_item_info[$v['item_id']]['model'];
                        $gongyingshang_res[$k]['status'] = $supply_item_info[$v['item_id']]['status'];
                        $gongyingshang_res[$k]['type'] = $supply_item_info[$v['item_id']]['type'];
                        $gongyingshang_res[$k]['code'] = $supply_item_info[$v['item_id']]['code'];    
                        $gongyingshang_res[$k]['name'] = $supply_item_info[$v['item_id']]['name'];    
                        $gongyingshang_res[$k]['unit_id'] = $supply_item_info[$v['item_id']]['unit_id'];    
                        $gongyingshang_res[$k]['sale_unit_id'] = $supply_item_info[$v['item_id']]['sale_unit_id'];    
                    }else{
                        $select_arr = [
                            'supply_item.id', 'supply_item.brand_id', 'supply_item.series_id', 'supply_item.model', 'supply_item.status', 
                            'supply_item.type', 'supply_item.unit_id', 'supply_item.sale_unit_id', 'supply_item.code', 'supply_item.name', 
                            'supply_item.retrieval_code', 'supply_item.erp_id',
                            'supply_brand.brand_name', 'supply_series.name AS series_name',
                        ];  //注意 耗材品牌、耗材系列 名称联查anhui_data库中的 (原因 库中有脏数据 名称匹配不上)
                        $item_id_info = SupplyItem::leftJoin('anhui_data.supply_brand', 'supply_item.brand_id', '=', 'supply_brand.id')
                                ->leftJoin('anhui_data.supply_series', 'supply_item.series_id', '=', 'supply_series.id')
                                ->where('erp_id', $v['item_id'])
                                ->select($select_arr)->first()->toArray(); //查询具体耗材的信息
                        $gongyingshang_res[$k]['si_id'] = $item_id_info['id'];   //erp id转换成 耗材id
                        $gongyingshang_res[$k]['brand_id'] = $item_id_info['brand_id'];
                        $gongyingshang_res[$k]['brand_name'] = $item_id_info['brand_name'];
                        $gongyingshang_res[$k]['series_id'] = $item_id_info['series_id'];
                        $gongyingshang_res[$k]['series_name'] = $item_id_info['series_name'];
                        $gongyingshang_res[$k]['model'] = $item_id_info['model'];
                        $gongyingshang_res[$k]['status'] = $item_id_info['status'];
                        $gongyingshang_res[$k]['type'] = $item_id_info['type'];
                        $gongyingshang_res[$k]['code'] = $item_id_info['code'];
                        $gongyingshang_res[$k]['name'] = $item_id_info['name'];
                        $gongyingshang_res[$k]['unit_id'] = $item_id_info['unit_id'];
                        $gongyingshang_res[$k]['sale_unit_id'] = $item_id_info['sale_unit_id'];
                    }
                    if(isset($supplier_info[$v['suppller_id']])){
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_info[$v['suppller_id']]['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_info[$v['suppller_id']]['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_info[$v['suppller_id']]['regular_pay'];//是否定期结算
                    }else{
                        $supplier_id_info = MaintenanceSupplier::where('erp_id', $v['suppller_id'])->first()->toArray(); //查询具体供应商的信息
                        $gongyingshang_res[$k]['supplier_id'] = $supplier_id_info['id'];    //供应商id  cms的供应商id
                        $gongyingshang_res[$k]['shop_name'] = $supplier_id_info['name'];    //供应商的名称
                        $gongyingshang_res[$k]['regular_pay'] = $supplier_id_info['regular_pay'];//是否定期结算
                    }
                    $gongyingshang_res[$k]['supplier_type'] = 2;    //供应商的类型
                }
            }
            //查询经销商平台 、康众 平台的报价数据 end
            
            $unit_list = DataSupplyUnit::getSupplyUnitList(0, 2);  //获取耗材单位数组
            
            //$all_list = array_merge($sc_list, $gongyingshang_res);
            $all_list = array_merge($gongyingshang_res, $sc_list);
            $supplier_arr   = [];           //供应商数组
            $unit_arr       = [];           //计量单位数组
            $sale_unit_arr  = [];           //商品单位数组
            $price_sort     = [];           //待排序的含税报价
            $storage_sort   = [];           //待排序的库存
            foreach ($all_list as $v) {
                $temp_1 = [
                    'supplier_id' => $v['supplier_id'],
                    'shop_name' => $v['shop_name'],
                ];
                $supplier_arr[$v['supplier_id']] = $temp_1;
                $temp_2 = [
                    'unit_id' => $v['unit_id'],
                    'unit_name' => $unit_list[$v['unit_id']],
                ];
                $unit_arr[$v['unit_id']] = $temp_2;
                $temp_3 = [
                    'sale_unit_id' => $v['sale_unit_id'],
                    'sale_unit_name' => $unit_list[$v['sale_unit_id']],
                ];
                $sale_unit_arr[$v['sale_unit_id']] = $temp_3;
                $price_sort[] = $v['tax_purchase_price'];
                $storage_sort[] = $v['current_number'];
            }
            //报价排序 [1:含税升;2:含税降;3:库存升;4:库存降;]
            switch ($sort) {
                case 1:
                    array_multisort($price_sort, $all_list);
                    break;
                case 2:
                    array_multisort($price_sort, SORT_DESC, $all_list);
                    break;
                case 3:
                    array_multisort($storage_sort, $all_list);
                    break;
                case 4:
                    array_multisort($storage_sort, SORT_DESC, $all_list);
                    break;
                default:
                    break;
            }
            
            $result = [
                'list' => $all_list,
                'supplier_arr' => array_values($supplier_arr),
                'unit_arr' => array_values($unit_arr),
                'sale_unit_arr' => array_values($sale_unit_arr),
            ];
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/queryPriceByName", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-6-5 上午9:59
     * @Description:     获取耗材品牌列表
     * @param string $token             用户token
     * @param string $name              品牌名称[支持模糊查询]
     * @param int $num                  一次展示的数量 [ 默认为全部 ]
     * @link http://api.anhui.com/v1/scs/purchase/supplyBrandList?token=34957948583f6a2e2aae3bca5b54b8c1&num=10&name=
     */
    public function supplyBrandList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'num' => 'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $name   = Input::get('name', '');   //品牌名称[支持模糊查询]
            $num    = Input::get('num', 0);     //一次展示的数量 [ 默认为全部 ]
            
            $list = DataSupplyBrand::select('id', 'initial', 'brand_name')->where('status', 1)->orderBy('id', 'ASC');
            if($name != ''){
                $list->where('brand_name', 'like', '%'.$name.'%');
            }
            if($num != 0){
                $list = $list->skip(0)->take($num)->get();
            } else {
                $list = $list->get();
            }
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['count' => count($list), 'list' => $list]);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/supplyBrandList", 'input' =>Input::all() ,"output" => $response));
        
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-6-5 上午10:39
     * @Description:     根据耗材的品牌获取耗材系列
     * @param string $token             用户token
     * @param int $brand_id             品牌id
     * @param int $num                  一次展示的数量 [ 默认为全部 ]
     * @link http://api.anhui.com/v1/scs/purchase/supplySeries?token=34957948583f6a2e2aae3bca5b54b8c1&brand_id=10&num=
     */
    public function supplySeries(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand_id' => 'required',
                'num' => 'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $brand_id   = Input::get('brand_id');
            $num        = Input::get('num', 0);     //一次展示的数量 [ 默认为全部 ]
            $list = DataSupplySeries::select('id', 'name', 'desc')->where('status', 1)->where('brand_id', $brand_id);
            if($num != 0){
                $list = $list->skip(0)->take($num)->get();
            } else {
                $list = $list->get();
            }
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['count' => count($list), 'list' => $list]);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "purchase/supplySeries", 'input' =>Input::all() ,"output" => $response));
        
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-9 下午3:00
     * @Description:     采购询价 -> 加入购物车
     * 直接下单、加入购物车的时候 校验保存初始 收货人信息 [该门店 收货人信息不存在时 ，使用门店信息 为店长id排序]
     * @param string $token             用户token
     * @param int $supplier_id          供应商id
     * @param int $supply_item_id       商品耗材id
     * @param float $num                订购数量    [默认值为1]
     * @param float $price              单价 [单位 分]
     * @param int $op_type              操作类型 [1:普通添加购物车;2:直接下单;]
     * @link http://api.anhui.com/v1/scs/purchase/addShoppingCar?token=4080b38991d929266a6da106b89e2e74&supplier_id=1&supply_item_id=11
     * @link http://api.anhui.com/v1/scs/purchase/addShoppingCar?token=077d83f2867b94111c643a78fc65e79b&supplier_id=526&supply_item_id=235262&price=1600&op_type=2&num=5
     */
    public function addShoppingCar() {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'supplier_id' => 'required',
                'supply_item_id' => 'required',
                'price' => 'required',
                'op_type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = $sc_user->shop_id;
            $admin_id = $sc_user->id;
            $now_time = date('Y-m-d H:i:s', time());
            
            $check_res = PMSReceivesAddress::checkShopDefaultAddress($shop_id, $admin_id);
            if(!$check_res){
                $response = Lang::get("scs_error.order_default_address_error_400141");
                break;
            }
            
            $supplier_id = Input::get('supplier_id');       //供应商id
            $supply_item_id = Input::get('supply_item_id'); //商品耗材id
            $num = Input::get('num', 1);                    //订购数量
            $price = Input::get('price');                   //加入购物车时单价
            $op_type = Input::get('op_type');               //操作类型 [1:普通添加购物车;2:直接下单;]
            
            try {
                $psc_info = PMSShoppingCar::where('shop_id', $shop_id)->where('supplier_id', $supplier_id)
                        ->where('status', 1)->where('supply_item_id', $supply_item_id)
                        ->first();
                if($psc_info){
                    //查询购物车 将要修改的耗材信息
                    $select_arr = [
                        'pms_shopping_car.id', 'pms_shopping_car.shop_id', 'pms_shopping_car.supplier_id', 
                        'pms_shopping_car.supply_item_id', 'pms_shopping_car.num', 
                        'supply_item.name AS supply_item_name', 'supply_item.erp_id AS item_erp_id',
                        'maintenance_supplier.name AS supplier_name', 'maintenance_supplier.erp_id AS supplier_erp_id',
                    ];
                    $info = DB::table('pms_shopping_car')
                            ->leftJoin('supply_item', 'pms_shopping_car.supply_item_id', '=', 'supply_item.id')
                            ->leftJoin('maintenance_supplier', 'pms_shopping_car.supplier_id', '=', 'maintenance_supplier.id')
                            ->where('pms_shopping_car.id', $psc_info->id)
                            ->where('pms_shopping_car.shop_id', $shop_id)
                            ->where('pms_shopping_car.status', 1)
                            ->select($select_arr)
                            ->first();
                    //查询经销商平台 耗材报价和库存
                    $query_price_info = GYSQueryPrice::getQueryPriceList([$info->item_erp_id], [$info->supplier_erp_id], $shop_id, 2);
                    if(empty($query_price_info)){
                        //查询出的耗材报价、库存 为空 返回提示
                        $response = Lang::get("scs_error.shopping_query_pirce_error_65013");
                        break;
                    }
                    $storage = $query_price_info[$info->supplier_erp_id][$info->item_erp_id]['storage'];      //经销商平台 对应耗材库存
                    if($op_type == 1){
                        $after_num = $psc_info->num + $num;     //变更之后的数量
                    } else {
                        $after_num = $num;     //变更之后的数量
                    }
                    //18-5-23 下午1:15  暂时不校验经销商平台 库存
//                    if($storage != -1 && $storage < $after_num){
//                        $response = Lang::get("scs_error.shopping_storage_less_65014");
//                        $response['storage'] = $storage;
//                        return $response;
//                    }
                    
                    $psc_info->num = $after_num;
                    $psc_info->price = $price;
                    $psc_info->updated_at = $now_time;
                    $psc_info->updated_by = $admin_id;
                    $psc_info->save();
                    
                    $car_res = $psc_info->id;
                }else{
                    $psc = new PMSShoppingCar();
                    $psc->shop_id = $shop_id;
                    $psc->supplier_id = $supplier_id;
                    $psc->supply_item_id = $supply_item_id;
                    $psc->num = $num;
                    $psc->price = $price;
                    $psc->status = 1;   //状态(1:正常;2:已删除;3:已下单;)
                    $psc->created_at = $now_time;
                    $psc->created_by = $admin_id;
                    $psc->save();
                    $car_res = $psc->id;    //返回购物车信息id
                }
                
                $response = array('code' => 0, 'msg' => 'ok', 'result' => ['id' => $car_res]);
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "addShoppingCar", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.data_inventory_isrun_61022");
                break;
            }
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "addShoppingCar", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-9 下午3:50
     * @Description:     采购询价 -> 购物车列表
     * @param string $token             用户token
     * @param int $id                   购物车id
     * @link http://api.anhui.com/v1/scs/purchase/shoppingCarList?token=077d83f2867b94111c643a78fc65e79b
     * @link http://api.anhui.com/v1/scs/purchase/shoppingCarList?token=077d83f2867b94111c643a78fc65e79b&id=78
     */
    public function shoppingCarList() {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $id = Input::get('id', '');     //购物车信息id
            $shop_id = $sc_user->shop_id;
            //查询的时候需要将 对应的耗材id、经销商id 转换为 erp id
            $select_arr = [
                'pms_shopping_car.id', 'pms_shopping_car.shop_id', 'pms_shopping_car.supplier_id', 
                'pms_shopping_car.supply_item_id', 'pms_shopping_car.num', 'pms_shopping_car.price AS car_price',
                'supply_item.name AS supply_item_name', 'supply_item.erp_id AS item_erp_id',
                'maintenance_supplier.name AS supplier_name', 'maintenance_supplier.erp_id AS supplier_erp_id', 
                'maintenance_supplier.regular_pay',
            ];
            $list = DB::table('pms_shopping_car')
                    ->leftJoin('supply_item', 'pms_shopping_car.supply_item_id', '=', 'supply_item.id')
                    ->leftJoin('maintenance_supplier', 'pms_shopping_car.supplier_id', '=', 'maintenance_supplier.id')
                    ->where('pms_shopping_car.shop_id', $shop_id)
                    ->where('pms_shopping_car.status', 1)
                    ->select($select_arr);
            if($id != ''){
                $list->where('pms_shopping_car.id', $id);
            }
            $list = $list->get();
            
            $res_list = [];     //返回结果数组
            $item_erp_arr = []; //耗材erpid数组
            $supplier_erp_arr = []; //供应商erpid数组
            if($list){
                foreach ($list as $v) {
                    $item_erp_arr[] = $v->item_erp_id;
                    $supplier_erp_arr[] = $v->supplier_erp_id;
                }
                $item_erp_arr = array_unique($item_erp_arr);
                $supplier_erp_arr = array_unique($supplier_erp_arr);
                //查询经销商平台 耗材报价和库存
                $query_price_list = GYSQueryPrice::getQueryPriceList($item_erp_arr, $supplier_erp_arr, $shop_id, 2);
                if(!empty($query_price_list)){
                    foreach ($list as $v) {
                        $temp_arr = [
                            'id' => $v->id,
                            'shop_id' => $v->shop_id,
                            'supply_item_id' => $v->supply_item_id,
                            'supply_item_name' => $v->supply_item_name,
                            'num' => $v->num,
                            'car_price' => $v->car_price,
                            'price' => isset($query_price_list[$v->supplier_erp_id][$v->item_erp_id]['price'])?$query_price_list[$v->supplier_erp_id][$v->item_erp_id]['price']:'',
                            'storage' => isset($query_price_list[$v->supplier_erp_id][$v->item_erp_id]['storage'])?$query_price_list[$v->supplier_erp_id][$v->item_erp_id]['storage']:'',
//                            'supplier_id' => $v->supplier_id,
//                            'supplier_name' => $v->supplier_name,
                        ];
                        $res_list[$v->supplier_id]['supplier_id'] = $v->supplier_id;
                        $res_list[$v->supplier_id]['supplier_name'] = $v->supplier_name;
                        //返回供应商 能够支持的 支付方式    [0:现金;1:定期挂账;]
                        // if($v->regular_pay == 1){
                        //     if(in_array($v->supplier_id, Config::get('config.kangzhong'))){
                        //         $res_list[$v->supplier_id]['regular_pay'] = ''.$v->regular_pay;
                        //     }else{
                        //         $res_list[$v->supplier_id]['regular_pay'] = '0,'.$v->regular_pay;
                        //     }
                        // }else{
                        //     $res_list[$v->supplier_id]['regular_pay'] = ''.$v->regular_pay;
                        // }
                        // 
                        if(in_array($v->supplier_id, Config::get('config.kangzhong'))){
                            $res_list[$v->supplier_id]['regular_pay'] = '1';
                        }else{
                            $res_list[$v->supplier_id]['regular_pay'] = ''.$v->regular_pay;
                            if($v->regular_pay == 2) {//安区域
                                $regular_pay = MaintenanceSupplierPayArea::where('supplier_id', $v->supplier_id)->where('city_id', $sc_user->shop->city_id)->first();
                                if (is_null($regular_pay)) {
                                    $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
                                    $response["msg"]= $response["msg"].$v->supplier_name;
                                    break 2;
                                } else {
                                    $res_list[$v->supplier_id]['regular_pay'] = ''.$regular_pay->regular_pay;
                                }
                            }elseif ($v->regular_pay == 3) {//安门店
                                $regular_pay = MaintenanceSupplierPayShop::where('shop_id', $sc_user->shop_id)->where('supplier_id', $v->supplier_id)->first();
                                if (is_null($regular_pay)) {
                                    $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
                                    $response["msg"]= $response["msg"].$v->supplier_name;
                                    break 2;
                                } else {
                                    $res_list[$v->supplier_id]['regular_pay'] =  ''.$regular_pay->regular_pay;
                                }
                            }
                        }                        
                        $res_list[$v->supplier_id]['item_list'][] = $temp_arr;
                    }
                }else{
                    //查询出的耗材报价、库存 为空 返回提示
                    $response = Lang::get("scs_error.shopping_query_pirce_error_65013");
                    break;
                }
            }
            
            //默认收货人信息
            $address_info = PMSReceivesAddress::where('shop_id', $shop_id)->where('status', 1)
                    ->select('id','shop_id','name','phone','address')->first();
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => ['list' => $res_list, 'address_info' => $address_info]);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "shoppingCarList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-9 下午5:30
     * @Description:     采购询价 -> 购物车编辑
     * @param string $token     用户token
     * @param string $id        购物车信息id [ 删除多条信息时 用英文逗号分隔， 例如: 15,16 ]
     * @param int $op_type      操作类型 1:修改;2:删除;
     * @param float $num        变更后的数量   例如 1
     * @link http://api.anhui.com/v1/scs/purchase/editShoppingCar?token=4080b38991d929266a6da106b89e2e74&id=1&op_type=2&num=2
     */
    public function editShoppingCar() {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
                'op_type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = $sc_user->shop_id;
            $admin_id = $sc_user->id;
            $now_time = date('Y-m-d H:i:s', time());
                     
            $id = Input::get('id');             //购物车信息id
            $op_type = Input::get('op_type');   //操作类型 1:修改;2:删除;
            $num = Input::get('num', '');       //变更数量   例如 2
            
            try {
                if($op_type == 1){      //修改
                    //查询购物车 将要修改的耗材信息
                    $select_arr = [
                        'pms_shopping_car.id', 'pms_shopping_car.shop_id', 'pms_shopping_car.supplier_id', 
                        'pms_shopping_car.supply_item_id', 'pms_shopping_car.num', 
                        'supply_item.name AS supply_item_name', 'supply_item.erp_id AS item_erp_id',
                        'maintenance_supplier.name AS supplier_name', 'maintenance_supplier.erp_id AS supplier_erp_id',
                    ];
                    $info = DB::table('pms_shopping_car')
                            ->leftJoin('supply_item', 'pms_shopping_car.supply_item_id', '=', 'supply_item.id')
                            ->leftJoin('maintenance_supplier', 'pms_shopping_car.supplier_id', '=', 'maintenance_supplier.id')
                            ->where('pms_shopping_car.id', $id)
                            ->where('pms_shopping_car.shop_id', $shop_id)
                            ->where('pms_shopping_car.status', 1)
                            ->select($select_arr)
                            ->first();
                    //查询经销商平台 耗材报价和库存
                    $query_price_info = GYSQueryPrice::getQueryPriceList([$info->item_erp_id], [$info->supplier_erp_id], $shop_id, 2);
                    if(empty($query_price_info)){
                        //查询出的耗材报价、库存 为空 返回提示
                        $response = Lang::get("scs_error.shopping_query_pirce_error_65013");
                        break;
                    }
                    $storage = $query_price_info[$info->supplier_erp_id][$info->item_erp_id]['storage'];      //经销商平台 对应耗材库存
                    
                    if($num != '' && $op_type == 1){
                        $response = DB::transaction(function() use ($shop_id, $id, $num, $now_time, $admin_id, $storage){
                            $psc = PMSShoppingCar::where('shop_id', $shop_id)->where('status', 1)->where('id', $id)->lockForUpdate()->first();
                            //18-5-23 下午1:15  暂时不校验经销商平台 库存
//                            if($storage != -1 && $storage < $num){
//                                $response = Lang::get("scs_error.shopping_storage_less_65014");
//                                $response['storage'] = $storage;
//                                return $response;
//                            }
                            $psc->num = $num;
                            $psc->updated_at = $now_time;
                            $psc->updated_by = $admin_id;
                            $res = $psc->save();
                            return array('code' => 0, 'msg' => 'ok', 'result' => $res);
                        });
                    }
                } else {          //删除
                    $id_arr = explode(',', $id);
                    
                    $update_data['status'] = 2;     //状态(1:正常;2:已删除;3:已下单;)
                    $update_data['updated_at'] = $now_time;
                    $update_data['updated_by'] = $admin_id;
                    
                    $response = DB::transaction(function() use ($shop_id, $id_arr, $update_data){
                        $res = DB::table('pms_shopping_car')
                                ->where('shop_id', $shop_id)
                                ->where('status', 1)
                                ->whereIn('id', $id_arr)
                                ->update($update_data);
                        return array('code' => 0, 'msg' => 'ok', 'result' => $res);
                    });
                }
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "editShoppingCar", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.data_inventory_isrun_61022");
                break;
            }
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "editShoppingCar", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-14 上午10:57
     * @Description:     采购询价  校验全部供应商报价
     * @param string $token         用户token
     * @param string $ids           购物车信息id的字符串  [多个值用英文逗号分隔 ,  例如: 1,2]
     * @link http://api.anhui.com/v1/scs/purchase/checkPrice?token=e17aa63fb641b26044f7a025960a5570&ids=14,15
     */
    public function checkPrice() {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'ids' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = $sc_user->shop_id;
            $ids = Input::get('ids');             //购物车信息id的字符串
            $id_arr = explode(',', $ids);
            
            //查询的时候需要将 对应的耗材id、经销商id 转换为 erp id
            $select_arr = [
                'pms_shopping_car.id', 'pms_shopping_car.shop_id', 'pms_shopping_car.supplier_id', 
                'pms_shopping_car.supply_item_id', 'pms_shopping_car.num', 'pms_shopping_car.price AS car_price',
                'supply_item.name AS supply_item_name', 'supply_item.erp_id AS item_erp_id',
                'maintenance_supplier.name AS supplier_name', 'maintenance_supplier.erp_id AS supplier_erp_id', 
                'maintenance_supplier.regular_pay',
            ];
            $list = DB::table('pms_shopping_car')
                    ->leftJoin('supply_item', 'pms_shopping_car.supply_item_id', '=', 'supply_item.id')
                    ->leftJoin('maintenance_supplier', 'pms_shopping_car.supplier_id', '=', 'maintenance_supplier.id')
                    ->where('pms_shopping_car.shop_id', $shop_id)
                    ->where('pms_shopping_car.status', 1)
                    ->whereIn('pms_shopping_car.id', $id_arr)
                    ->select($select_arr)
                    ->get();
            $res_list = [];     //返回结果数组
            $item_erp_arr = []; //耗材erpid数组
            $supplier_erp_arr = []; //供应商erpid数组
            if($list){
                foreach ($list as $v) {
                    $item_erp_arr[] = $v->item_erp_id;
                    $supplier_erp_arr[] = $v->supplier_erp_id;
                }
                $item_erp_arr = array_unique($item_erp_arr);
                $supplier_erp_arr = array_unique($supplier_erp_arr);
                //查询经销商平台 耗材报价和库存
                $query_price_list = GYSQueryPrice::getQueryPriceList($item_erp_arr, $supplier_erp_arr, $shop_id, 2);
                if(!empty($query_price_list)){
                    foreach ($list as $v) {
                        $temp_arr = [
                            'id' => $v->id,
                            'shop_id' => $v->shop_id,
                            'supply_item_id' => $v->supply_item_id,
                            'supply_item_name' => $v->supply_item_name,
                            'num' => $v->num,
                            'car_price' => $v->car_price,
                            'price' => isset($query_price_list[$v->supplier_erp_id][$v->item_erp_id]['price'])?$query_price_list[$v->supplier_erp_id][$v->item_erp_id]['price']:'',
                            'storage' => isset($query_price_list[$v->supplier_erp_id][$v->item_erp_id]['storage'])?$query_price_list[$v->supplier_erp_id][$v->item_erp_id]['storage']:'',
                            'supplier_id' => $v->supplier_id,
                            'supplier_name' => $v->supplier_name,
                            'regular_pay' => $v->regular_pay,
                        ];
                        $res_list[$v->id] = $temp_arr;
                    }
                }else{
                    //查询出的耗材报价、库存 为空 返回提示
                    $response = Lang::get("scs_error.shopping_query_pirce_error_65013");
                    break;
                }
            }
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $res_list);
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "checkPrice", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:          zk
     * @DateTime:        18-5-14 上午10:57
     * @Description:     采购询价 -> 收货人信息编辑
     * @param string $token     用户token
     * @param int $id           收货人信息id
     * @param string $name      姓名
     * @param string $phone     手机号
     * @param string $address   收货地址
     * @link http://api.anhui.com/v1/scs/purchase/editReceivesAddress?token=8303c4c8210e5957893389f0acbfd9c1&id=1
     */
    public function editReceivesAddress() {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user = $this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $shop_id = $sc_user->shop_id;
            $admin_id = $sc_user->id;
            $now_time = date('Y-m-d H:i:s', time());
                     
            $id = Input::get('id');
            $name = Input::get('name', '');
            $phone = Input::get('phone', '');
            $address = Input::get('address', '');
            
            try {
                $response = DB::transaction(function() use ($shop_id, $id, $name, $phone, $address, $admin_id, $now_time){
                    $info = PMSReceivesAddress::where('shop_id', $shop_id)->where('id', $id)->lockForUpdate()->first();
                    if($name != ''){
                        $info->name = $name;
                    }
                    if($phone != ''){
                        $info->phone = $phone;
                    }
                    if($address != ''){
                        $info->address = $address;
                    }
                    $info->updated_at = $now_time;
                    $info->updated_by = $admin_id;
                    $res = $info->save();
                    return array('code' => 0, 'msg' => 'ok', 'result' => $res);
                });
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "editReceivesAddress", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.data_inventory_isrun_61022");
                break;
            }
        }while(false);
        
        Log::info("info", array("system" => "scs" ,"event" => "editReceivesAddress", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    

}